Vim

This is a simple cheatsheet for Vim shortcuts. Whenever I find a new interesting shortcut which I have know idea before, I will add it to the sheet. Feel free to create push request with new shortcuts.

Cheatsheet

Cheatsheet image source

Closing file

Shortcut Description
Ctrl + c “You have no power here!!!”
:q close VIM
:q! force close VIM
:x save and close VIM
:wq save and close VIM
:wqa save and close all tabs VIM
ZZ save and close VIM

Global

Shortcut Description
:h keyword open help for keyword
Ctrl + ] open link in help
:w [filename] save file
K open manual page for word under the cursor

Cursor movement

Always try to use [number] and commands. For example to move 4 lines down use 4j.

Shortcut Description
h move cursor left
j move cursor down
k move cursor up
l move cursor right
gh move cursor left (don’t jump over wrapped line)
gj move cursor down (don’t jump over wrapped line)
gk move cursor up (don’t jump over wrapped line)
gl move cursor right (don’t jump over wrapped line)
H move to top of screen
M move to middle of screen
L move to bottom of screen
zz center cursor on screen
w jump forwards to the start of a word
W jump forwards to the start of a word (words can contain punctuation)
e jump forwards to the end of a word
E jump forwards to the end of a word (words can contain punctuation)
b jump backwards to the start of a word
B jump backwards to the start of a word (words can contain punctuation)
% move to matching character (default supported pairs: (), {}, []
^ jump to the first non-blank character of the line
$ jump to the end of the line
* jump to the next occurrence of the word under cursor
gg jump to the first line of the document
G jump to the last line of document
f[character] jump to next occurrence of the [character]
F[character] jump to previous occurence of the [character]
t[character] jump one character before next occurrence of the [character]
T[character] jump one character before previous occurrence of the [character]
; repeat previous f, t, F of T
, repeat previous f, t, F of T, backwards
} jump to next paragraph (or function/block)
{ jump to previous paragraph (or function/block)
Ctrl + b move back one full screen
Ctrl + f move forward one full screen
Ctrl + d move back 1/2 screen
Ctrl + u move forward 1/2 screen
Ctrl + o go back where you came from (from buffer, to last jump point, …)
Ctrl + i jump forward (from buffer, to last jump point, …)

Insert mode

Because Esc is used very often, think about swap with CapsLock.

Shortcut Description
Esc exit insert mode
i insert before the cursor
I insert at the beginning of the line
a insert (append) after the cursor
A insert (append) at the end of the line
o append (open) a new line below the current line
O append (open) a new line above the current line
ea insert (append) at the end of the word
Ctrl + o break insert mode for one command

Editing

What about mapping Ctrl + r to the U?

Shortcut Description
r replace a single character
J join line below to the current one with one space in between
gJ join line below to the current one without space in between
cc change (replace) entire line
cw change (replace) to the end of the word
ciw change (replace) the whole word
caw change (replace) the whole word with the space at the end
D delete from current to the end of the line
C delete from current to the end of the line and jump to insert mode
c$ change (replace) to the end of the line
s delete character and substitute text
S delete line and substitute text (same as cc)
xp transpose two letters (delete and paste)
u undo
Ctrl + r redo
. repeat last command

Visual mode

Shortcut Description
Esc exit visual mode
v start visual mode, mark lines, then do a command (like y-yank)
V start linewise visual mode
o move to other end of marked area
Ctrl + v start visual block mode
O move to other corner of block
aw mark a word
ab a block with ()
aB a block with {}
ib inner block with ()
iB inner block with {}
gv mark last marked block
> shift text right
< shift text left
y yank (copy) marked text
d delete marked text
~ switch case

Marks

Use it. I mean it really, USE IT! Try plugin vim-signature

Shortcut Description
:marks list of marks
ma set current position for mark A
'a jump to position of mark A
y'a yank text to position of mark A

Registers

See Vim registers tutorial

Shortcut Description
"[character]y yank selected text to [character]
"[character]p paste [character] register
Ctrl + r[character] paste [character] register in insert mode
:reg see all the registers and their content

Special registers:

  • "" - last deleted text (with d, c, s or x)
  • "[number] - last 10 yank or deleted text ("0 the newest, "9 the oldest)
  • ". - last insert text (read only)
  • "% - current file path (read only)

Macros

Shortcut Description
q[character] record macro [character]
q stop recording macro
@[character] run macro [character]
@@ rerun last run macro

Cut and paste

As always use [number] and command. What about mapping y$ to the Y?

Shortcut Description
yy yank (copy) a line
yw yank (copy) the characters of the word from the cursor position to the start of the next word
y$ yank (copy) to end of line
p put (paste) the clipboard after cursor
P put (paste) before cursor
dd delete (cut) a line
dw delete (cut) the characters of the word from the cursor position to the start of the next word
dW delete (cut) the characters of the word from the cursor position to the start of the next word (words can contain punctuation)
x delete (cut) character

Search and replace

Use sed pattern replace as you are used to. You can mark text in Visual mode and then apply replace patter with :[command].

Shortcut Description
\* search for the next occurrence of the word under cursor
/[pattern] search for pattern
?[pattern] search backward for pattern
\[pattern] ‘very magic’ pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
n repeat search in same direction
N repeat search in opposite direction
:%s/old/new/g replace all old with new throughout file
:%s/old/new/gc replace all old with new throughout file with confirmations
:noh remove highlighting of search matches

Buffers

See my .vimrc for mapping For undestanding differencies between buffers and tabs see Buffers vs tabs

Shortcut Description
:e file edit a file in a new buffer
:args open multiple files in a new buffers
:bnext or :bn go to the next buffer
:bprev or :bp go to the previous buffer
:bd delete a buffer (close a file)
:ls list all open buffers

Split window

Try set splitbelow and set splitright for more natural split.

Shortcut Description
:sp [file] open a file in a new buffer and split window
:vsp [file] open a file in a new buffer and vertically split window
Ctrl + ws split window
Ctrl + ww switch windows
Ctrl + wq quit a window
Ctrl + wv split window vertically
Ctrl + wh move cursor to the left window (vertical split)
Ctrl + wl move cursor to the right window (vertical split)
Ctrl + wj move cursor to the window below (horizontal split)
Ctrl + wk move cursor to the window above (horizontal split)

Tabs

Shortcut Description
:tabnew [file] open a file in a new tab
Ctrl + wT move the current split window into its own tab
gt move to the next tab
gT move to the previous tab
:tabmove [number] move current tab to the [number]th position (indexed from 0)
:tabclose or :tabc close the current tab and all its windows
:tabonly or :tabo close all tabs except for the current one
:tabdo [command] run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

Folding

See Folding tutorial and wiki

Shortcut Description
za toggle fold (open/close fold)
zA toggle all folds (open/close fold)
zo open fold at the cursor
zO open all folds at the cursor
zc close fold at the cursor
zC close all folds at the cursor
zf#j creates a fold from the cursor down # lines
zf/string creates a fold from the cursor to string
zj moves the cursor to the next fold
zk moves the cursor to the previous fold
zm increases the foldlevel by one
zM closes all open folds
zr decreases the foldlevel by one
zR decreases the foldlevel to zero – all folds will be open
zd deletes the fold at the cursor
zE deletes all folds
[z move to start of open fold
]z move to end of open fold

Commands

See commands

Shortcut Description
:! [command] run [command]
:r [file] read [file] and insert it under cursor
:r! [command] run [command] and the result insert under cursor
% ! [command] run command on whole document and replace it with the command result

Tags

See tags tutorial

Shortcut Description
Ctrl + ] jump to the tab location
Ctrl + o jump back to the starting point

Spellcheck

Useful links: Usage Spell Check Compile Dictonary-cs

Setup:

:set spell spelllang=en_us
:setlocal spell spelllang=en_us "Only for local buffer
:set nospell
Shortcut Description
] + s jump to next misspelled word
[ + s jump to previous misspelled word
z + = open suggest list

Others

Shortcut Description
gf open file under cursor in new buffer
Ctrl + wgf open file under cursor in new tab
gg=G format/indent document
Ctrl + a Increase number under cursor
Ctrl + x Decrease number under cursor
g + Ctrl + a Increase number under cursor (like arithmetic line)
g + Ctrl + x Decrease number under cursor (like arithmetic line)