VIM Tricks

This is a VIM Tricks compilation not so obvious, this is meant to be used as a reference document for some vim tricks.

The g prefix

gf Open the file under the cursor.

gd Go to definition of a variable.

gi Open insert mode in the last insertion spot.


:%s/\%V{pattern}/{replacement}/{g|c|i} Let you replace ONLY the selection \%V and not necessarily whole lines.

=G Fix the indentantion in the whole file.

:w !sudo tee % When you realize you opened a file that you don't have permissions to edit.

set wildignore+=*/node_modules set wig+=*/node_modules To ignore the node modules folder so when you use args and argdo it won't select the node modules

]I get the lines where word under cursor appears.

C-G get current buffer filename 1<C-G>get current file full path.

Insert modei_CTRL-X_CTRL-F to show files/folders in the current working directory and if you want list hidden .<C-x><C-f>.And then use Ctrl+P and Ctrl+N to change between results.


Grepping inside VIM

Remember to:set wig+=*/node_modules/**/* to avoid showing results inside node_modules

:vimgrep /{pattern}/g {%|**/*}

And then use :copen to show the results.

Making immediate ESC and C-[ to switch modes

Add to the ~/.vimrc file:

set timeoutlen=1000 ttimeoutlen=0

And then into the ~/.tmux.conffile:

set -sg escape-time 0

If you want to make a non greedy search

Instead of .*(greedy) use .\{-}(non greedy)

For example: Lets say you want to parse<a href="#">Foo</a>

But you don't want them to match <a href="#" target="_blank">Foo</a>

Naturally you'll try to do: :%s/<a href=".*">/.../gci

Instead do: :%s/<a href=\(".\{-}"\)>/<a href=\1 target="_blank">/gci For some reason, this doesn't work, apparently because > matches end of word. Neovim Forum topic opened

What I did in the end to solve the problem in other way is: :%s/<a href=\(".{-}"\).{-}>/<a href=\1 target="_blank">/gci

Deleting empty lines:

What :[range]g[lobal]/{regex}/{Ex-command} :g/^$/d . To see the list of Ex-command, type :h ex-cmd-index