Modes
Esc
Return to Normal mode (the home base)
a
Insert mode after cursor
o
New line below, enter Insert
O
New line above, enter Insert
Ctrl+v
Visual Block mode (column selection)
R
Replace mode (overwrites)
:
Ex command mode (e.g. :w, :q)
Save & Quit
:w <file>
Write to a different file
:q
Quit (fails if unsaved changes)
:q!
Quit, discarding changes
:x
Write (only if changed) and quit
ZZ
Same as :x (Normal mode shortcut)
ZQ
Same as :q! (Normal mode shortcut)
:wa
Write all open buffers
Cursor Motion
h j k l
Left, down, up, right
w
Forward to next word start
b
Back to previous word start
^
First non-blank char on line
{ }
Previous/next paragraph (blank-line separated)
%
Jump to matching bracket / paren
Ctrl+f
Full page down (forward)
Ctrl+b
Full page up (back)
f<c>
Forward to next occurrence of char on line
F<c>
Backward to previous occurrence
t<c>
Forward UNTIL char (stops one before)
Editing
x
Delete char under cursor
X
Delete char before cursor (backspace)
d$
Delete to end of line (same as D)
d0
Delete to start of line
cc
Change (delete and enter Insert) line
r<c>
Replace single char with c
~
Toggle case of current char
J
Join current line with next
Text Objects (combine with d, c, y, v)
iw
Inner word (diw = delete word)
aw
A word (with surrounding whitespace)
i" or i'
Inside quotes (ci" = change inside quotes)
i( or i)
Inside parentheses
it / at
Inside / around HTML/XML tag
ip / ap
Inside / around paragraph
Pattern to internalize: {operator}{count}{motion}. d2w = delete 2 words. y3j = yank 3 lines down. c$ = change to end of line. Once this clicks, every other Vim trick gets cheap.
Search & Replace
:s/foo/bar/
Replace first match on current line
:s/foo/bar/g
Replace all on current line
:%s/foo/bar/g
Replace all in file
:%s/foo/bar/gc
Replace all with confirmation
:%s/foo/bar/gi
Replace all, case-insensitive
:5,15s/foo/bar/g
Replace on lines 5-15
:'<,'>s/foo/bar/g
Replace in visual selection (auto-filled if selected first)
Useful Settings (~/.vimrc)
:set number
Show line numbers
:set relativenumber
Relative line numbers (great with motions)
:set expandtab
Use spaces instead of tabs
:set ignorecase
Case-insensitive search
:set smartcase
Case-sensitive when search has uppercase
:set clipboard=unnamedplus
Use system clipboard by default (Linux)
:syntax on
Syntax highlighting