Using c.vim hotkeys - vim

I use c.vim plugin for vim. Different hotkeys generate part of code which should be quickly modifiable. For example (there in no cursor tag, it's the place where cursor is located after hotkey):
for (*<cursor>* ; {+CONDITION+}; {+INCREMENT+} ) {
}
How to quickly modify things in curly braces?

If you like the automatic snippet insertion, there are now a couple of general plugins which are not limited to C files (but many ship with built-in snippets for C, C++, Java, etc., and all of them are extensible by you). Those may be a more powerful alternative.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

If the cursor is on or in {+CONDITION+} you can use ci{ or ca{ for change inner block or change a block.
Change inner block leaves the curly brackets and change a block does not.
Take a look at :h text-objects and :h i{

Related

How to can I make a title shortcut

I use vim and I want that in every file I will have a title like
**year=
**author=
**
**
..
I want to map a key for past it in the head of a file.
Their is any plugin for this?
Thanks.
Well, you don't need a plugin just to map a key to insert some text at the beginning of the current buffer:
:nno \h ggO**year=<cr>**author=<cr>**<cr>**<esc>
Just change \h to your
preferred key sequence.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

How do I customize three letter sequences in Vim Latex-Suite?

I installed Latex-Suite for Vim, and I like it very much, but I'd like to be able to customize the environment mappings that came by default, and add new ones. For example I want to edit the equation environment that appears typing EEQ and move around some elements, like the \label{} command. How can I do this? I've been scanning everything inside my /usr/share/vim/vimfiles/ftplugin but I can't find a way to do it (or I just don't understand what those files are).
You want to check out the documentation on Macro Customisation, specifically the Tex_Env_{name} bit.
In short, if you want your theorem snippet to look like
\begin{theorem}
<++>
\end{theorem}<++>
then you want a line like
let g:Tex_Env_theorem = "\\begin{theorem}\<CR><++>\<CR>\\end{theorem}"
in your vimrc.
Note the backslashes to escape carriage-return, and double-backslash for normal backslashes.
The <F5> functionality (press F5 after typing an environment name, i.e. figure<F5>) should work out of the box, but you may need to refresh the three-letter code. This is more hassle than it needs to be, but something like
autocmd BufNewFile,BufRead *.tex call IMAP('EFI', g:Tex_Env_figure,'tex')
will do the job.
The answer to the question you asked comes with a caveat, which is that Latex-Suite is an enormous amount of code that is very hard and annoying to modify, and which does not play nicely with other plugins. This falls into Latex-Suite's philosophy that it's the only plugin you need for editing latex within vim.
That said, you want to look in /path/to/ftplugin/latex-suite/envmacros.vim. Searching for EEQ will lead you on the path to understanding the set of calls that latex-suite performs. I would like to reiterate that many functions are deeply intertwined.
On the other hand, there is a very easy way to have very easily customizable environments, which are snippets. See the UltiSnips page for a good example of how this works. These are designed for customization and extremely easy to write.

How to understand code with vim?

Vim is a nice editor and I use it for many tasks.
However, when it comes to start working on a new (possibly huge) codebase, I don't feel comfortable using it to go around the code with the objective of understanding how things work.
For example, if I want to see where a C++ function is used, I can :vimgrep for that function in every **/*.cpp files and :copen the quickfix window to jump on every occurrence... of that string.
If I do the same with e.g. Eclipse (call hierarchy of a C++ method), that will not be just a string, but a C++ method defined in an object, so I will get a precise indication of the usages of that function (and not also a function with the same name defined in another class).
So the question is, how to make vim a powerful tool to analyze code?
Subquestions:
Are there any vim plugins designed for this?
Does it make sense to use vim to only analyze code? Probably external tools (e.g.: OpenGrok) can do the job?
Vim is a text editor. What you want is almost completely orthogonal to editing text and completely outside of Vim's own abilities.
However, Vim is quite good at using external tools like ctags and cscope for navigating within a project. Supposing you have created a tags file and/or a cscope.out database, Vim has a bunch of commands you can use to "jump to definition", "jump to usage", etc.: :ts[elect] foo, <C-]> over a function name and so on… You can find all the info you need in :h ctags and :h cscope.
If you are curious, GNU GLOBAL is another alternative. Another pro of cscope is that it comes with its own TUI that you can use in your shell.
The only plugin offering a cscope (+ ctags) interface I know of is CCTree which seems to be limited to C.
There are a bunch of ctags oriented plugins like TagBar or TagList you could try but note that, while ctags is limited to definitions, cscope can also do usage and callers.
You should keep in mind that these tools are code indexers: you shouldn't expect them to "understand" your code or be even remotely as precise as IDE tools. However I love Vim, I'd suggest you use a tool better suited to that task than a text editor.
When I switched from Netbeans to Vim, I felt the same as you. I missed Netbeans feature to see any function definition upon a right click. Disclaimer: I use Ruby, Javascript mainly and sometimes PHP
I tried ctags, but found it is not so accurate and not so clean. I also tried plugins Tagbar and Taglist using together with ctags. Tagbar is a bit heavy in my opinion, takes lot of CPU and memory when handling tags. Taglist is better, but the best use case is to browse long files instead of tags.
Finally I gave up using ctags.
Later I found there are better solutions specific to language. For example, for Ruby there is a plugin to show ri doc of any function within installed gems(libs), with only one key.
But I still don't use that often as my habit already changed. I like things to be lean, to be in there right places, and to be fast.
Now I feel comfortable by:
Using tmux together with Vim. Check doc and verify code in console when needed.
Use snippet plugin(Neosnippet) to store frequently used codes, methods. The snippets management in Vim is far better and flexible than any IDE I've seen.
Use brain to store more, with less touching of mouse.
Hope these help.
There is a plugin called fly.vim that is super awesome to browse source code. It makes use of a cscope database and provides simple navigation mechanism. Combine it with autotags plugin that generates and maintains cscope and ctags for a project in a central location, and you can switch between different code bases with ease.
I was using Source Insight to browse the Linux Kernel source code and when I switched over to this combination, I had nothing to complain. It may take some time and/or effort to get in speed with this setup, though. If you know how ctags and cscope work, then, probably you'll pick it up in less than an hour. But the advantages: cscope indexes code fast, vim uses cscope fast, fly.vim queries through cscope and displays it fast, and in a usable format. Plus, it maintains jump history.

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)

How to create short snippets in Vim?

I have recently started using Vim as my text editor and am currently working on my own customizations.
I suppose keyboard mappings can do pretty much anything, but for the time being I'm using them as a sort of snippets facility almost exclusively.
So, for example, if I type def{TAB} (:imap def{TAB} def ():<ESC>3ha), it expands to:
def |(): # '|' represents the caret
This works as expected, but I find it annoying when Vim waits for a full command while I'm typing a word containing "def" and am not interested in expanding it.
Is there a way to avoid this or use this function more effectively to this end?
Is any other Vim feature better suited for this?
After taking a quick look at SnippetsEmu, it looks like it's the best option and much easier to customize than I first thought.
To continue with the previous example:
:Snippet def <{}>():
Once defined, you can expand your snippet by typing def{TAB}.
Snipmate - like texmate :)
http://www.vim.org/scripts/script.php?script_id=2540
video:
http://vimeo.com/3535418
snippet def
""" ${1:docstring} """
def ${2:name}:
return ${3:value}
As another suggestion (although slightly different) using vim's built in functionality:
:iabbrev def def(): #<LEFT><LEFT><LEFT><LEFT><LEFT>
Now whenever you type def followed by a space or other non-word character, it will expand to the same as what you've given as the output of SnippetsEmu (the space comes from the space you entered to trigger the completion).
This approach doesn't suffer the "lag" issue you encountered using :inoremap, and is built-into vim. For more information on this feature, look at :help abbrev.
You may be concerned that being triggered by space not tab it will trigger unnecessarily, but in general vim is pretty smart about when to trigger it. The issue can be additionally mitigated by enabling the abbreviation only for certain file-types (eg, python):
au filetype python :iabbrev ... etc
Snip[ets] (Manager|Emu|Mate|.vim) is of course also a perfect solution, but it's nice to be aware of the alternatives (especially when they are built in).
If SnippetsEmu is too heavy or ambitious for what you need (it was for me), I wrote a plugin that manages snippets based on filetype. It even has tab completion when picking the snippet! :)
Get it here: snippets.vim
I just installed UltiSnips. There’s a good article that explains why you might choose UltiSnips: Why UltiSnips?
I haven’t used any of the other snippet plugins; I decided to take the plunge with one that seemed full-featured and would be able to accommodate me as I gain more Vim skills and want to do more sophisticated things.
SnippetsEmu is a useful snippets plugin.
As noted by MDCore, SnippetsEmu is a popular Vim script that does just that and more. If you need only expanding (without moving back the caret), you can use the standard :ab[breviate] command.
:ab[breviate] [<expr>] {lhs} {rhs}
add abbreviation for {lhs} to {rhs}. If {lhs} already
existed it is replaced with the new {rhs}. {rhs} may
contain spaces.
See |:map-<expr>| for the optional <expr> argument.

Resources