Published on

Vim Cheat Sheet (Master Vim)

VIM: CheetSheet

What is Vim?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficiently.

This Vim cheat sheet will show you all useful vim commands that anyone can use for their reference.

Global commands in vim

:h[elp] keywordopen help for keyword
:clo[se]close current pane
:ter[minal]open a terminal window
Kopen man page for word under the cursor

Editing commands in vim

rreplace a single character.
Rreplace more than one character, until ESC is pressed.
Jjoin line below to the current one with one space in between
gJjoin line below to the current one without space in between
gwipreflow paragraph
g~switch case up to motion
guchange to lowercase up to motion
gUchange to uppercase up to motion
ccchange (replace) entire line
c$ or Cchange (replace) to the end of the line
ciwchange (replace) entire word
cw or cechange (replace) to the end of the word
sdelete character and substitute text
Sdelete line and substitute text (same as cc)
xptranspose two letters (delete and paste)
uundo
Urestore (undo) last changed line
Ctrl + rredo
.repeat last command

Cursor movement commands in vim

hmove cursor left
jmove cursor down
kmove cursor up
lmove cursor right
gjmove cursor down (multi-line text)
gkmove cursor up (multi-line text)
Hmove to top of screen
Mmove to middle of screen
Lmove to bottom of screen
wjump forwards to the start of a word
Wjump forwards to the start of a word (words can contain punctuation)
ejump forwards to the end of a word
Ejump forwards to the end of a word (words can contain punctuation)
bjump backwards to the start of a word
Bjump backwards to the start of a word (words can contain punctuation)
gejump backwards to the end of a word
gEjump backwards to the end of a word (words can contain punctuation)
%move to matching character (default supported pairs: '()', '', '[]' - use :h matchpairs in vim for more info)
0jump to the start of the line
^jump to the first non-blank character of the line
$jump to the end of the line
g_jump to the last non-blank character of the line
gggo to the first line of the document
Ggo to the last line of the document
5gg or 5Ggo to line 5
gdmove to local declaration
gDmove to global declaration
fxjump to next occurrence of character x
txjump to before next occurrence of character x
Fxjump to the previous occurrence of character x
Txjump to after previous occurrence of character x
;repeat previous f, t, F or T movement
,repeat previous f, t, F or T movement, backwards
}jump to next paragraph (or function/block, when editing code)
{jump to previous paragraph (or function/block, when editing code)
zzcenter cursor on screen
ztposition cursor on top of the screen
zbposition cursor on bottom of the screen
Ctrl + emove screen down one line (without moving cursor)
Ctrl + ymove screen up one line (without moving cursor)
Ctrl + bmove back one full screen
Ctrl + fmove forward one full screen
Ctrl + dmove forward 1/2 a screen
Ctrl + umove back 1/2 a screen

Insert mode commands in vim -

iinsert before the cursor
Iinsert at the beginning of the line
ainsert (append) after the cursor
Ainsert (append) at the end of the line
oappend (open) a new line below the current line
Oappend (open) a new line above the current line
eainsert (append) at the end of the word
Ctrl + hdelete the character before the cursor during insert mode
Ctrl + wdelete word before the cursor during insert mode
Ctrl + jbegin new line during insert mode
Ctrl + tindent (move right) line one shiftwidth during insert mode
Ctrl + dde-indent (move left) line one shiftwidth during insert mode
Ctrl + ninsert (auto-complete) next match before the cursor during insert mode
Ctrl + pinsert (auto-complete) previous match before the cursor during insert mode
Ctrl + rxinsert the contents of register x
Ctrl + oxTemporarily enter normal mode to issue one normal-mode command x.
Esc or Ctrl + cexit insert mode

Marking text commands in vim (visual mode)

vstart visual mode, mark lines, then do a command (like y-yank)
Vstart linewise visual mode
omove to other end of marked area
Ctrl + vstart visual block mode
Omove to other corner of block
awmark a word
aba block with ()
aBa block with
ata block with <> tags
ibinner block with ()
iBinner block with
itinner block with <> tags
Esc or Ctrl + cexit visual mode

Visual commands in vim

>shift text right
<shift text left
yyank (copy) marked text
ddelete marked text
~switch case
uchange marked text to lowercase
Uchange marked text to uppercase

Registers commands in vim

:reg[isters]show registers content
"xyyank into register x
"xppaste contents of register x
"+yyank into the system clipboard register
"+ppaste from the system clipboard register

Macros commands in vim

qarecord macro a
qstop recording macro
@arun macro a
@@rerun last run macro

Cut and paste commands in vim

yyyank (copy) a line
2yyyank (copy) 2 lines
ywyank (copy) the characters of the word from the cursor position to the start of the next word
yiwyank (copy) word under the cursor
yawyank (copy) word under the cursor and the space after or before it
y$ or Yyank (copy) to end of line
pput (paste) the clipboard after cursor
Pput (paste) before cursor
gpput (paste) the clipboard after cursor and leave cursor after the new text
gPput (paste) before cursor and leave cursor after the new text
dddelete (cut) a line
2dddelete (cut) 2 lines
dwdelete (cut) the characters of the word from the cursor position to the start of the next word
diwdelete (cut) word under the cursor
dawdelete (cut) word under the cursor and the space after or before it
:3,5ddelete lines starting from 3 to 5
:g/{pattern}/ddelete all lines containing pattern
:g!/{pattern}/ddelete all lines not containing pattern
d$ or Ddelete (cut) to the end of the line
xdelete (cut) character

Indent text commands in vim

>>indent (move right) line one shiftwidth
<<de-indent (move left) line one shiftwidth
>%indent a block with () or (cursor on brace)
<%de-indent a block with () or (cursor on brace)
>ibindent inner block with ()
>atindent a block with <> tags
3==re-indent 3 lines
=%re-indent a block with () or (cursor on brace)
=iBre-indent inner block with
gg=Gre-indent entire buffer
]ppaste and adjust indent to current line

Exiting commands in vim

:wwrite (save) the file, but don't exit
:w !sudo tee %write out the current file using sudo
:wq or :x or ZZwrite (save) and quit
:qquit (fails if there are unsaved changes)
:q! or ZQquit and throw away unsaved changes
:wqawrite (save) and quit on all tabs

Search and replace commands in vim

/patternsearch for pattern
?patternsearch backward for pattern
\vpattern'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
nrepeat search in same direction
Nrepeat search in opposite direction
:%s/old/new/greplace all old with new throughout file
:%s/old/new/gcreplace all old with new throughout file with confirmations
:noh[lsearch]remove highlighting of search matches

Search in multiple files commands in vim

:vim[grep] /pattern/ {file}search for pattern in multiple files
`e.g. :vim[grep] /foo/ /*
:cn[ext]jump to the next match
:cp[revious]jump to the previous match
:cope[n]open a window containing the list of matches
:ccl[ose]close the quickfix window

Tabs commands in vim

:tabnew or :tabnew {page.words.file}open a file in a new tab
Ctrl + wTmove the current split window into its own tab
gt or :tabn[ext]move to the next tab
gT or :tabp[revious]move to the previous tab
#gtmove to tab number #
:tabm[ove] #move current tab to the #th position (indexed from 0)
:tabc[lose]close the current tab and all its windows
:tabo[nly]close all tabs except for the current one
:tabdo commandrun the command on all tabs (e.g. :tabdo q - closes all opened tabs)

Working with multiple files commands in vim

:e[dit] fileedit a file in a new buffer
:bn[ext]go to the next buffer
:bp[revious]go to the previous buffer
:bd[elete]delete a buffer (close a file)
:b[uffer]#go to a buffer by index #
:b[uffer] filego to a buffer by file
:ls or :bufferslist all open buffers
:sp[lit] fileopen a file in a new buffer and split window
:vs[plit] fileopen a file in a new buffer and vertically split window
:vert[ical] ba[ll]edit all buffers as vertical windows
:tab ba[ll]edit all buffers as tabs
Ctrl + wssplit window
Ctrl + wvsplit window vertically
Ctrl + wwswitch windows
Ctrl + wqquit a window
Ctrl + wxexchange current window with next one
Ctrl + w=make all windows equal height & width
Ctrl + whmove cursor to the left window (vertical split)
Ctrl + wlmove cursor to the right window (vertical split)
Ctrl + wjmove cursor to the window below (horizontal split)
Ctrl + wkmove cursor to the window above (horizontal split)
Ctrl + wHmake current window full height at far left (leftmost vertical window)
Ctrl + wLmake current window full height at far right (rightmost vertical window)
Ctrl + wJmake current window full width at the very bottom (bottommost horizontal window)
Ctrl + wKmake current window full width at the very top (topmost horizontal window)

Diff commands in vim

zfmanually define a fold up to motion
zddelete fold under the cursor
zatoggle fold under the cursor
zoopen fold under the cursor
zcclose fold under the cursor
zrreduce (open) all folds by one level
zmfold more (close) all folds by one level
zitoggle folding functionality
]cjump to start of next change
[cjump to start of previous change
do or :diffg[et]obtain (get) difference (from other buffer)
dp or :diffpu[t]put difference (to other buffer)
:diffthismake current window part of diff
:dif[fupdate]update differences
:diffo[ff]switch off diff mode for current window