Vim
I’ve been using vim for more than 10 years. Here are a list of things I usually forget from time to time that are extremely useful.
- You can use
gi
in normal mode to start inserting in the last position you inserted even if you have moved through the buffer. - To emulate the power of multiple cursors we can make use of the
cgn
text object:- Search a word with
/
or*
- Use
cgn
to change the word - Use dot repeat to keep going or use
gn
so skip a word
- Search a word with
- Fuzzy find files with
:find *
. This works on other directories as well, not only the root:find ../../../**/woah
- Use
:b whatever
to switch to an open buffer in vim with a whatever substring in the file name. - Tags navigation is awesome:
- Go to next tag:
C-]
- Find ambiguous tag:
gC-]
- Go back:
C-t
- Go to next tag:
- We don’t necessarily need completion plugins. Instead here are some commands for more powerful completion:
- Simple completion with:
C-n
orC-p
- Restrict to current file:
C-x C-n
orC-x C-p
- Complete only file names:
C-x C-f
- Restrict to tags only:
C-x C-]
- Use omnicompletion
C-x C-o
- Complete definition:
C-x C-d
- Complete from included file:
C-x C-i
- Complete line already present in file:
C-x C-l
- Simple completion with:
- Working with the quickfix window:
- Open it with:
:copen
- Open it only if there are errors, otherwise close it:
:cw
- Close it:
:ccl
- Go to next error:
:cn
- Go to previous error:
:cp
- Filter entries by a pattern with:
:Cfilter
or for exclusion:Cfilter!
- Execute an action on the items from the quickix:
:cdo s/foo/bar/ | update
- When on the quickfix window, press
<CR>
to open on the top window or<C-W><CR>
to open on a new window.
- Open it with:
- Use
gf
to open the file under the cursor. - You can use vim as a mergetool, if configured:
git mergetool
- Move to merge conflict on the bottom window
- Keep local changes with
:diffget LOCAL
or:diffg LO
- Keep local changes with
:diffget REMOTE
or:diffg RE
- Navigate conflicts with
C-w,j
- After saving, commit the changes.
- Searching files is amazing with just
grep
orvimgrep
commands. They all fill the quickfix window.- Search local buffer:
:vimgrep /pattern/ %
- Search from the current working directory:
:vimgrep /pattern/ **
or:Vimgrep /pattern/
- Use an external tool for grepping (i.e.
ripgrep
)::Grep /pattern/
- Use
:Ggrep
if interested only in filtering by the files on the git repository.
- Search local buffer:
- Using the plugin
vim-unimpaired
is very practical:- Jump between quickfix items:
]q
or[q
- Jump between spelling errors:
]s
or[s
- Add space before/after the line:
]<space>
or]<space>
- Jump between buffers:
]b
or[b
- Jump between files in the directory:
]f
or[f
- Exchange lines up/down:
]e
or[e
- Jump between quickfix items:
- Increase/decrease numbers by using
<C-A>
and<C-X>
respectively.