Vim: good pascal support (syntax, indent, probably omnicomplete) - vim

I need to work on existing freepascal project ( it's an open-source project double commander ), so I'm trying to set Vim for editing source files, but pascal support bundled with Vim is surprisingly ugly.
More, I can't even find anything for better pascal support. At least, I really need for working syntax and indent scripts (both of them are buggy by default).
It would be very nice to set up omnicompletion too, but it seems this is completely hopeless.
I will probably hack on syntax and indent scripts myself, but I can't believe we still don't have them, so I decided to ask first.

Perhaps you can improve the situation by telling Vim to use the "delphi" mode instead of the "pascal" mode (this trick works with Emacs too). The reason for this is that in general "Pascal" refers to the old language designed by Wirth in the '70, while Free Pascal incorporates many syntactical additions introduced by Turbo Pascal and Delphi in the following years. Most of the development on Vim/Emacs modes usually goes in the Delphi mode, not the Pascal one.
Have a look at http://www.vim.org/scripts/script.php?script_id=3078. This will not provide autocompletion, but at least it will highlight keywords like "unit", "interface", "implementation", etc., as well as // comments, correctly.

As VIM 7.4.52 for Linux there isn't 'Delphi' option. So far, the 'Pascal' mode works good.

for a freepascal project (fpc), you need to get fpc.vim.
Doing it with 'Plug'
open your .vimrc file and add:
call plug#begin('~/.vim/plugged')
Plug 'vim-scripts/fpc.vim'
call plug#end()
reopen your vim, and use the command :PlugInstall
Highlighting
in order to highlight pascal use command:
:set filetype=pascal
this also enables indent for the code. and it will auto indent the existing code.
or you can use:
:set syntax=pascal
this may fail to indent.

Related

How can I use vim plugins with Ideavim?

I would like to use Tim Pope's vim surround plugin in my Pycharm IDE. I've been using the IdeaVim plugin for Pycharm to use vim motions and commands.
I know I can use ~/.ideavimrc like my normal .vimrc but I cannot find
information about how to use plugins with ideavim.
Can I specify the plugins directory inside my ~/.ideavimrc or do I have to go another way? Can I use a plugin manager like pathogen?
The latest version of IdeaVim includes the vim-surround plugin. Enable it by adding
set surround
to your .ideavimrc file
https://github.com/JetBrains/ideavim#emulated-vim-plugins
Most applications only emulate Vim's / vi key bindings (and often only the basic navigation and editing commands). That goes a long way to helping vi users edit comfortably, but it isn't the real thing.
Unfortunately, to be able to use Vim plugins, you'll need the full Vimscript interpreter and infrastructure around 'runtimepath'. I'm not aware of any application that provides this, and because of the complexities and idiosyncrasies of Vim, this would be very hard indeed.
To get more of Vim's special capabilities into your IDE, use both concurrently; it is very easy to set up a external tool in your IDE that launches Vim with the current file (and position). Automatic reloading of changes allows you to edit source code in both concurrently.
If it's any comfort to you, the same applies to Emacs / Elisp as well.
The ideaVim plugin added "Support for vim-surround commands ys, cs, ds, S, enable it with set surround in your ~/.ideavimrc" since version 0.46.
No. Vote for VIM-506 for Vim scripts support (unlikely to be implemented) and for VIM-769 for vim-surround emulation (likely to appear in future versions).
This was mentioned in a changelog relatively recently.
https://github.com/JetBrains/ideavim/blob/master/CHANGES.md#features-5
I didn't mange this work. Looks like I need vim-plug installed and I use another package manager in my NeoVim and also I don't want to mix up two configs. It should work in general.

Refactoring in Vim

Of course the fact that you can refactor on IDEs is priceless for many, I hardly ever do it when I am coding but I may try to do it when editing some one else's source. How do you accomplish such a trivial task across multiple files in Vim?
I found this plugin for refactoring Ruby, but how about "any" language?
I agree with the 'Vim is not an IDE' paradigm. But there are times when there isn't an IDE. Here's what I use in those situations:
Disclaimer: The ubiquity of Language Server Protocol servers, linters and fixers since I wrote this have also brought some great refactoring capabilities to Vim (and other editors). IMO they are a long way from equaling the capabilities of a purpose-built IDE (I prefer ALE and nvim-lspconfig for these kinds of features). See other answers on this question for more info!
:grep, :vimgrep, :GrepperAg, :Ggrep
Refactoring that has more to do with regular replacements I usually use :grep on my project tree and then record a macro to do the refactor - :g and :s are no brainers. Usually it'll let me quickly modify a large number of files with very little effort. Honestly, I use this method more than any other.
Depending on your workflow the built-in commands might be slow/inconvenient. If you use git, then you'll wanna use the excellent Fugitive plugin and its :Ggrep command to only search files checked into git. I also like the vim-grepper because it is search-tool-agnostic (supports ag, sift, ripgrep, etc) and speedy.
:argdo, :cdo, and :bufdo
:cdo and :argdo are handy to execute vim commands over a set of files.
command line
When it's harder to determine the list of files that need changes via :vimgrep I resort to the command line grep/find commands to more closely curate the list of files that I need to refactor. Save the list to a text file and use :e and a mashup of macro recordings to make the changes I need to make.
I find that the less rusty I keep my macro recording skills the more useful I find Vim for refactoring: feeling comfortable saving/restoring from registers, incrementing/decrementing register counter variables, cleaning/saving macro recordings to file for later use, etc.
Update
Since writing this more videocasts for the methods I describe have been published on vimcasts.org (I encourage you to watch ALL the Vimcasts!). For refactoring watch these ones:
Substitution with :Subvert
Project wide search/replace
Search multiple files with :vimgrep
Use :argdo to change multiple files
Vimgolf is also a great way to practice.
Language Server Protocol (LSP)
The Language server protocol contains the feature for smart renaming of symbols across a project:
https://microsoft.github.io//language-server-protocol/specifications/specification-3-14/#textDocument_rename
For example following language server support this:
Clangd for C++
ccls for C/C++/Objective-C
Eclipse.jdt.ls for Java
pyls (with rope) for Python
tsserver for TypeScript
Solargraph for Ruby
gopls official lsp for Go (alpha stage in Nov 2019)
texlab for LaTeX
You can find more language servers under https://langserver.org/.
Vim
A vim editor client is necessary to use them within vim. Following options exist:
LanguageClient-neovim (requires rust) suggests the mapping:
nnoremap <silent> <F2> :call LanguageClient_textDocument_rename()<CR>
coc.nvim (requires node.js) suggests the mapping:
" Remap for rename current word
nmap <leader>rn <Plug>(coc-rename)
Ale has
nnoremap <silent> <Plug>(ale_rename) :ALERename<Return>
Ale does not define any keybindings. This has to be done by the user.
vim-lsp provides following command
:LspRename
Similar to Ale no mapping is suggested. However, of course you can define one as following
nmap <leader>r <plug>(lsp-rename)
(<leader>r is to be replaced by your choice; I do not know one which most plugins agree on)
vim-lsc has a default mapping:
'Rename': 'gR'
See also YouCompleteMe which facilitates LSPs as well.
Neovim
Neovim has initial builtin support for lsp since 13.11.2019
See for common configurations of LSPs the project nvim-lspconfig which suggests <space>rn as a mapping for vim.lsp.buf.rename().
Other Refactorings
I do not know if there are plans for the LSP protocol to support more complex refactorings, such as changing class structure, adding parameters to methods/functions or moving a method to a different class. For a list of refactorings see https://refactoring.com/catalog/.
Python
For the python language following plugins provide 'smart' renaming capabilities for vim:
jedi-vim (github) <leader>r
ropevim (github) CTRL-c r r
python-mode (github) :h pymode-rope-refactoring
C-Family
Try the plugin Clighter for rename-refactoring for the c-family. It is based on clang, but there are limitations and the plugin is marked as deprecated.
Suggested mapping by Clighter is
nmap <silent> <Leader>r :call clighter#Rename()<CR>
Note, the successor plugin clighter8 has removed the renaming functionality in the commit 24927db42.
If you use neovim, you can take a look at the plugin clamp. It suggests
nmap <silent> <Leader>r :call ClampRename()<CR>
Maybe not the most elegant solution, but I found it very handy: I use ECLIM to connect VIM and Eclipse. Of course all my source code editing is done in VIM, but when it's time to refactor, one can take advantage of Eclipse's superior cababilities in this matter.
Give it a try.
I wrote this plugin for generic refactoring. It still requires many improvements. Sometime in the future I'll try to abandon ctags in favour of clang for C&C++ refactorings.
Plugin YouCompleteMe (YCM) (20k stars on github)
http://ycm-core.github.io/YouCompleteMe/#the-refactorrename-new-name-subcommand
:h RefactorRename-new-name
In supported file types, this command attempts to perform a semantic
rename of the identifier under the cursor. This includes renaming
declarations, definitions and usages of the identifier, or any other
language-appropriate action. The specific behavior is defined by the
semantic engine in use.
Similar to FixIt, this command applies automatic modifications to your
source files. Rename operations may involve changes to multiple files,
which may or may not be open in Vim buffers at the time. YouCompleteMe
handles all of this for you. The behavior is described in the
following section.
Supported in filetypes: c, cpp, objc, objcpp, cuda, java, javascript,
typescript, rust, cs
By default there is no mapping.
Plugin Factorus
There is another vim plugin dedicated for refactoring called factorus which is available on github.
Currently (2017-12), it supports the languages
c,
java, and
python.
Place cursor at name to refactor and type
gd (or gD if you're refactoring a global variable).
Then
cgn new_name esc
and
. one or more times to refactor next occurrence(s)
or
:%norm . to refactor all occurrences in the buffer at once.
I write a lot of C/C++ code in vim. The most common refactoring that I do is renaming variables, class names, etc. Usually, I use :bufdo :%s/source/dest/g to do a search/replace in files, which is almost the same as renaming provided by big IDE's.
However, in my case, I found that I usually rename similar entities, spelled in different cases (i.e CamelCase, snake_case, etc.), so I decided to write a small utility to help with this kind of "smart-case" search/replace, it is hosted here. It is a command-line utility, not a plugin for vim, I hope that you can find it useful.
Go
The tool godoctor (github) supports several refactoring capabilities
Rename
Extract Function
Extract Local Variable
Toggle var ⇔ :=
Add Godoc stubs
There is a vim plugin https://github.com/godoctor/godoctor.vim which makes them available
With cursor in thing to rename:
:Rename <newname>
Highlighting block to extract:
:Refactor extract newfunc
vim-go
Precise type-safe renaming of identifiers with :GoRename.
Language server gopls
https://github.com/golang/tools/blob/master/gopls/doc/status.md#rename
For refactoring, if you're using Unite (and you should), you can then use vim-qfreplace and make it extremely easy. Check this video that demonstrates how it works. Once your workflow is set, you can make some mappings to optimize it (instead of typing most things like in the video).
A combination of two plugins: vim-ripgrep, to find across files and put the results in the quickfix window, and quickfix-reflector to save the changes right in the quickfix window and have it automatically save each change across the files.
I would consider using the spacemacs version of emacs. It is uses the same modes and most keystrokes as Vim but has many more add-on because of it's lisp nature. If you want to program in C++ you just add the c++ layer and most of the IDE is just set up for you already. For other interpreted languages like python or bash you do not need to leave spacemacs to use them. They even have a way to run blocks of code directly within your text which works fantastic for literate programming or reproducible programming where the code and the data are in the same file. Both done as text.
Spacemacs is much more heavy handed in it's initial load but the additional stuff you can do with it is worth the few seconds of startup cost. One layer org-mode is worth checking it out. It is the best outliner, programmer, day timer / todo list I have ever used.
The CoC addon has (among other features) the ability to rename variables.
https://github.com/neoclide/coc.nvim
" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)

Programming Scheme(Racket) with VIM - How to get started

recently, I started programming Racket (formerly Scheme) in DrRacket.
I quite fast I began to miss all the features of VIM in DrRacket, so I would like
to use VIM for my scheme(racket) programming.
I know that Emacs might be the best choice for intense lisp programming, but all
I want is write a scheme(racket) file check syntax and then run it.
Unfortunately, I could not figure out, how to invoke "racket" in the commandline
on a file to get it doing the same as DrRacket.
I am running Ubuntu 10.10 Maverick Meerkat, VIM 7.3 and I downloaded and
installed Racket from the official website.
Help to get started would be very appreciated.
If you just want to load a file into Racket on the command-line and run it, I think that this should work:
$ racket -f file.scm -i
The -i option would leave you at the REPL to test your code in file.scm.
However, you might want to take a look at this blog post:
http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/
You can set up a GNU screen session running the command-line Racket, and send s-expressions to it from Vim. There's a link to a bit of Vimscript which will make this automatic. I used this for a while with mit-scheme, and it was reasonably effective. It's not quite the as powerful as SLIME or DrRacket though. I just tested the steps described on OS X and it works with the command-line racket, it should work on Ubuntu, that's what I was using when I first used this method.
You might also want to take a look at this:
http://evalwhen.com/scmindent/index.html
... which has some information on better indentation in Vim for Lisp and Scheme code.
I eventually switched to Emacs, but don't let the Vim haters get you down. Paul Graham supposedly uses vi and he's gotta be one of the top 5 most prominent Lisp programmers.
http://paul.graham.usesthis.com/
Several add-ons are available to make coding in Racket more satisfying with Vim.
Vim’s lisp mode (set lisp) -- improves some basics for indenting and w/e/b word-hopping.
vim-racket -- sizable set of vim-syntax recognition for functions and syntax
syntastic -- identify errors upon save (vim-racket sets makeprg for this to work)
Rainbow Parentheses -- set your own paren colors for maximal contrast!
vim-slime -- use with Tmux to connect Vim to a Racket REPL
surround -- wrap forms and elements in parens
vim-sexp for normal people
Molokai color scheme -- high contrast
VROD* -- K-based Vim documenation, and auto-completion of Racket functions
REPTL* -- very simple continuous rackunit test runner
I prefer slime over slimv for its simplicity.
If you are using Vundle, here is a way to install most of the add-ons mentioned:
Plugin 'tpope/vim-surround.git'
Plugin 'kien/rainbow_parentheses.vim'
Plugin 'scrooloose/syntastic.git'
Plugin 'MicahElliott/vrod'
Plugin 'guns/vim-sexp'
Plugin 'tpope/vim-sexp-mappings-for-regular-people'
Plugin 'wlangstroth/vim-racket'
Plugin 'tomasr/molokai.git'
Put those into your ~/.vimrc, and then invoke to install:
% vim +PluginInstall +qall
Here is a screenshot showing off most of these:
(* disclosure: I wrote VROD and REPTL, and they are immature)
Also, here is a related blog post describing a Vim/SICP setup.
As of about a week ago, SLIMV (SLIME for Vim) supports Racket. SLIMV is generally regarded as the most mature Lisp environment for Vim at the moment.

How do you use indent in vim for web development?

I'm starting to use Linux and Vim at work. I'm started reading vims documentation and creating my own .vimrc file and such.
I'm a web developer working with HTML, XML, CSS, JS, Python, PHP, ZPT, DTML and SQL.
I would like to have an indent feature like this one: for each language/set, a corresponding indent solution.
So, in js, writing function test(){|} would turn in
function test(){
|
}
If php, writing <?php function test(){|}:
<?php
function test(){
|
}
?>
...and such. Writing a function definition in Python, and then creating a for loop sentece, it would automatically create an indent.
I'm starting with autoindent, smartindent, cindent but I'm a little confused about their differences.
How do the indent in vim works? Am I supposed to download plugins for each language? Is the behavior I described possible with already existing plugins you're used to or do I have to create it?
I keep seeing people using Vim and I'm trying to do this as well since the machine I'm using is too limited, but I'm afraid I won't be able to have a decent auto indenting solution in it. And I really think that having to manually indent code all the time (instead of sometimes only) is a waste of time and it's against vim's "MOTTO" I've seen called "productivity".
(I have used autoindenting in a little small project in Visual Studio, and really liked their approach. Is there a plugin for that?)
Vim is usually pretty smart about indenting once you define the correct settings for tab size and the like. (Edit: As Igor mentions in the other answer, be sure to turn on filetype-specific indenting.) It seems that you want vim to automatically insert newlines though, which I don't think it can do without a plugin.
However, I think you may want to look at snipMate, which is a plugin that defines a large number of 'snippets' for different programming languages, and you can also define your own. It's basically a kind of improved tab-completion:
One example:
php<tab>
turns into
<?php
|
?>
With | being your cursor. Some snippets even define multiple cursor-positions which you can switch to with another press of tab.
vim usually comes with a bunch of syntax plugins for different languages. if you want to use those for indenting, you will need:
set autoindent
filetype indent on
you might also need syntax on but i'm not sure if that's needed for indenting. couldn't hurt though...
I found the setup for this based on a blog post:
set autoindent
inoremap {<CR> {<CR>}<Esc>O<Tab>
Having this with snipmate.vim and autoclose.vim is working flawlessly.

Auto formatting for vi?

Does an auto-formatting tool exist for vi that'll allow me to define per language preferences?
edit: I'm not looking for syntax highlighting. I'm looking for something that will apply formatting rules to my code. (Like brace positioning, spaces around oeprators, etc)
Well, there's Vim which comes with a lot of languages covered already and which is easy to customize per language.
Vim has tons of support for filetype-specific customisations. You might find what you are looking for in there.
You can add a file in ~/.vim/ftplugin/ for each file type. For example, set ~/.vim/ftplugin/c.vim to
set tabstop=2 shiftwidth=2
This sets your indentation for C files to two spaces.
You can use vim. If you're on GNU/Linux, take a look at /etc/vim/vimrc for global defaults. Some things you may want are "syntax on" "filetype indent on" and "set showmatch".
There is a vim plugin that enables formatting on your code from within vim. It's called vim-autoformat and you can dowload it here:
https://github.com/vim-autoformat/vim-autoformat
It integrates external code-formatting programs into vim. For example, if you want to format C, C++, C# or Java code, you can install the program astyle, and vim sets it as the format program automatically.
As Darrin says, "flee from the vi wasteland" and embrace the one true vim path instead! Your desired language preferences, assuming that they're not for SNOBOL or Simula, will thank you!
Edit: Actually extending the syntax highlighting to cover SNOBOL or Simula would not be that hard! (-:

Resources