My collection of vim tips to make the best editor even better. This is by no means complete or a tutorial on how to use vim, but a set of commands I don't want to forget and need to write them down before they burn into memory.
See the resources section below for a more complete introduction and a set of in-depth tutorials.
:nn " Jump to line nn
nn| " Jump to column nn
H M L " Jump dorectly to Highest, Middle, Lowest of screen
( ) { } " Beginning, End of current sentence, paragraph
0 ^ $ g_ " First, Last of current column, line
f F t T " Follow, Till forward, backward
, ; " Repeat last Follow/Till forward, backward
ma " mark spot current file label it a
mA " mark spot globally label it A
'a " jump to spot
'A " jump to spot (cross file)
'' " jump to last spot you were
'. " jump to last modification
:marks " show all marks
i I " Insert before cursor, BOL
a A " Append after cursor, EOL
o O " Open new line after, before current line
c{motion} C|cc " Change (delete and Insert mode) character, up to EOL
s S " Substitute (delete and Insert mode) char, line
J " join all the lines together.
< > " indent to the left, right
= " auto indent
y d ~ " yank, delete, change case of marked text
I, A, $ etc. will work too.
u Ctrl+r " undo, redo
J " join line below to the current one
y[movement] " copy
Y|yy " copy line
"ad " cut something to register a
p P " Paste before, after
"ap " paste something from register a
"0-9p " paste from delete history
:reg " list registers
"" " Default register for any yank or delete
". " Last inserted text
"+ " System clipboard
"/ " Search content
"= " Expression result
More about registers: https://www.brianstorti.com/vim-registers/
dd " delete line
D " delete up to EOL
:g/regexp/d " delete all lines that match regexp
:v/regexp/d " delete all lines that do NOT regexp
:v/w{3,}/d " delete all lines with less than 3-chars
:15,20d " delete lines 10-20
:ls " list open buffers
:b [num|name] " switch to buffer
:b# " switch to last buffer
:bd # " delete buffer
:*e*dit [file] " open file in new buffer
:sp file " open a file in a new split window
:vs file " open a file in a new vertical split window
Ctrl + wq " quit a window
Ctrl + w[dir] " move to direction (hjkl←↓↑→) window
Ctrl + ww " switch window
Ctrl + [+|-] " Grow, shrink split
:tabnew | :tabnew file " open a file in a new tab
Ctrl + wT " move the current split window into its own tab
gt | :tabn " move to the next tab
gT | :tabp " move to the previous tab
#gt " move to tab number #
:tabclose | :tabc " close the current tab and all its windows
qa " start recording macro in buffer a
[do stuff]
q " end recording
@a
50@a (50 times)
:vimgrep /search-pattern/[jg] file-pattern
:vimgrep /kateb/j **/*.go
:cn[ext] | :cp[revious] " jump to next and previous match
:cnf[ile] | :cpf[ile] " jump to next and previous file
:cw[indow] " open quickfix window (list of matches)
More options on vimgrep matches : https://stackoverflow.com/a/7880681/228648
<C-n> | <C-p> " Autocomplete from next|prev matches
<C-x><C-o> " Language aware autocomplete
Map ctrl-j d to run system command /tmp/x.py
:imap <C-j>d <C-r>=system('/tmp/x.py')<CR>
:map <F5> :setlocal spell! spelllang=en_us<CR>
Here are a set of short cuts I have in my vimrc file that simplify some common operations. If I notice myself doing the same thing over and over, I try to add a shortcut when possible.
" Add spaces inside parentheses, WordPress coding style
map <Leader>o ci(hp
" Surround word with quote
map <Leader>' ysiw'
map <Leader>" ysiw"
" Add Trailing Semi-colon
map <Leader>; g_a;<Esc>
" Use :w!! to save with sudo
ca w!! w !sudo tee >/dev/null "%"
One of the best things about vim is all of the available plug-ins. Here is the set of plug-ins I use that make vim even easier and better.
Tabular.vim is a very useful plugin to lineup characters amongst several lines, for example lining up the equal sign in a set of variable assignments. See vimcasts on tabular
CtrlP.vim is a fuzzy file matching finder for vim. It works similar to the Command-T plugin, a copy of Textmate’s file search. CtrlP is easier to setup and works real nice.