Vim Tips

Vim is awesome. I’ve been using it for over a year as my go-to all purpose editor.

Here are some tips and tricks in Vim I’ve learned over time.

This post only scratches the surface of Vim: I don’t talk about motions, marks, macros, or advanced techniques. If you’ve mastered these basics and are interested in learning more, check out this awesome answer from Stack Overflow.


Here are some basic Vim commands.


Command Effect
:w write (save) the file
:wq or :x or ZZ write (save) and quit
:q quit, fails if there are unsaved changes
:q! or ZQ quit and discard unsaved changes


Command Effect
h move cursor left
j move cursor down
k move cursor up
l move cursor right
w move to next word
b move to previous word
0 jump to beginning of line
$ jump to end of line
gg go to first line of file
G go to last line of document
Ctrl + u move up one-half screen
Ctrl + d move up one-half screen
% while on parentheses/braces: jump to matching parenthesis or brace

Insert mode: inserting text

Command Effect
i insert before cursor
I insert at beginning of line
a insert (append) after cursor
A insert (append) at end of line
o open a new line below current line
O open a new line above current line
Esc exit insert mode


Command Effect
r replace character
J join next line with current line
s delete character and enter insert mode
S or cc delete line and enter insert mode
u undo
Ctrl + r redo

Cut and paste

Command Effect
yy yank (copy) a line
y$ yank (copy) to end of line
x delete (cut) character
dd delete (cut) a line
d$ delete (cut) to end of line
p put (paste) after cursor
P put (paste) before cursor

Visual mode: marking text

Command Effect
v start visual mode (mark text)
V start visual line mode
vab mark a block with ()
vaB mark a block with {}
vib mark an inner block with ()
viB mark an inner block with {}
v-mode: y yank (copy) marked text
v-mode: d delete marked text
v-mode: ~ switch case of marked text
Esc exit visual mode

Search and replace

Command Effect
/pattern search for pattern
n repeat search
N repeat search in opposite direction
:%s/old/new/g replace all old with new in file
:%s/old/new/gc replace all old with new in file with confirmation
:noh remove highlighting of search matches

Cool stuff

Here are some more advanced tips and tricks.

Command Effect
. repeat previous command
q: view vim command history
:! <command> perform external command
:! ls perform ls command
:r! <command> perform external command and read output into file
:r! pbpaste (for Mac) paste system clipboard into file
gg=G automatically indent all lines in file
gv reselect last visual selection
g; move forward in changelist
g, move backward in changelist
'. go to last edited line
`. go to last edited position
:%TOhtml save current file as a HTML document