vim cheats

Vim Cheat Sheet

Searching

/pattern            search for pattern forward
?pattern            search for pattern backward
*                   search for highlighted (visual) pattern
n                   repeat search in the same direction
N                   repeat search in the opposite direction
:noh                remove search highlighting

Replacing

:%s/old/new/g       replace all occurrences of 'old' with 'new' throughout the file
:%s/old/new/gc      replace all occurrences with confirmation
:5,12s/old/new/g    replace all occurrences between lines 5 and 12
:s/old/new/g        replace all occurrences in the current line

Project-Wide Replace

:vim[grep] /pattern/ **/*         search pattern in all files under current directory
:cw                               open quickfix window with search results
:cfdo %s/old/new/ge | update      replace across files listed in quickfix window

Buffers

:e filename         open file in buffer
:e!                 discard changes in current buffer
:bufdo e!           discard changes in all buffers
:ls or :buffers     list all buffers
:bn or :bnext       switch to next buffer
:bp or :bprev       switch to previous buffer
:bd or :bdelete     close current buffer
:bufdo bd           close all buffers but current
:b N                go to specific buffer N
:b name             go to specific buffer by name
:q                  close current buffer
:qa                 close all buffers
:qa!                close all buffers without saving
:wa                 save all buffers
:x                  save and exit

Windows

:split or ctrl-w s      split window horizontally
:vsplit or ctrl-w v     split window vertically
:close                  close current window
:new or :vnew           open buffer in new window

Default Keymaps

ctrl-w h/j/k/l      move current window
ctrl-w n            create a new unpopulated window
ctrl-w s            split window horizontally
ctrl-w v            split window vertically
ctrl-w w            navigate between windows
ctrl-w c            close current window
ctrl-w o            close all other windows
ctrl-w r            rotate windows down/right
ctrl-w r            rotate windows up/left
ctrl-w =            equalize window sizes
ctrl-w +            increase window height
ctrl-w -            decrease window height
ctrl-w >            increase window width
ctrl-w <            decrease window width

Tabs

:tabnew or :tabe filename   open new tab
:tabn or :tabnext           switch to next tab
:tabp or :tabprev           switch to previous tab
:tabc or :tabclose          close current tab
:tabonly                    close all tabs but current
:tabn N                     move to specific tab - where N == tab number
:tabm N                     move current tab - N == new position
:tab split                  open current buffer in new tab

Default Keymaps

gt      move to the next tab.
gT      move to the previous tab.

References