Show Menu
Cheatography

Vi and Vim Cheat Sheet (DRAFT) by

Useful vi/vim editing commands that work on the OS X platform.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Navigating the file

h
Left one space
j
Down one line
k
Up one line
l
Right one space
0
Beginning of line (zero)
$
End of line
w, nw
Beginning of next word (n words) or punctu­ation.
W, nW
Beginning of next word (n words)
b, nb
Back one word or punctu­ation mark.
B, nB
Back one word
^f
Forward one screen
^b
Back one screen
^d
Down half a screen
^u
Up half a screen
^L
Redraw the screen
H, nH
Top of screen, n lines below top
M
Middle of screen
L, nL
Last line on screen, n lines above last line
e
End of word, or punctu­ation
E
End of word
(
Beginning of current sentence
)
Beginning of next sentence
{
Beginning of current paragraph
}
Beginning of next paragraph
[[
Beginning of current section
]]
Beginning of next section
 

Editing

i
Insert at cursor location
a
Append from cursor location
cw
Change from cursor to end of word
c$, C
Change from cursor to end of line
c0
Change from cursor to beginning of line
cnb
Change n words back
cc
Change entire line
r
Replace a single character
x
Delete current character
dw
Delete word
d/pattern
Delete from cursor to pattern
d$, D
Delete from cursor to end of line
dd
Delete current line
ndd
Delete n lines
J
Join current line with next line
u
undo last edit
CTRL-r
redo; undo the undo

Search and Replace

/pattern
Search forward for pattern
?pattern
Search backward for pattern
n, //
Repeat last search
nG, :n
Got to line n
G
Go to end of file
:%s/old/new/g
Search for old, replace with new, globally
 

Writing to multiple files

vi file1 file2
Start editing file1
:w
Write changes to file1
:n
Start editing next file
:x
Save file2 and exit vi

Editing with more than one file

:w
Save current file
:e filename
Open and edit new file
:e!
Discard edits and return to first file
:e #
Edit (return) to first filename
% is current filename
# is alternate filename
:w
write current file to
filena­me.new

2 ways to Yank & Put between files

r15yy
Yank 15 lines to buffer r
:w
Write (save) current filename
:e filename.next
Edit new file
rp
Put lines from buffer r
:5,12ya a
Yank lines 5 thru 12, save to buffer a
:pu a
Put lines from buffer a

Yank lines to a named buffer

f8yy
Yank 8 lines to buffer f
fp
Put lines from buffer f

Moving and saving

yw
Yank word
ynw
Yank n words
y$, yy
Yank current line
nyy
Yank n lines
p
Put last yanked
:w
Write file but don't exit
:w filename
Write file to filename
:x
Write and exit file