Vi Cheat Sheet

Window motions
CTRL d         — Scroll down (half a screen)
CTRL             — Scroll up (half a screen)
CTRL f         — Page forward
CTRL b         — Page backward
/string     — Search forward
?string     — Search backward
n                 — Repeat search
N                 — Repeat search reverse
G                 — Go to last line
nG                 — Go to line n
:n                 — Go to line n
CTRL l         — Redraw screen
CTRL g         — File information

Cursor motions
H                 — Upper left corner (home)
M                 — Middle line
L                 — Lower left corner
h                 — Back a character
j                 — Down a line
k                 — Up a line
g[k|j]         — Vertical line traverse
^                 — Beginning of line
$                 — End of line
l                 — Forward a character
w                 — Forward one word
b                 — Back one word
fc                 — Find c
;                 — Repeat find (find next c)

Input commands (end with ESC)
a                 — Append after cursor
i                 — Insert before cursor
o                 — Open line below
O                 — Open line above
:r file     — Insert file after current line

Changes during insert mode
CTRL h         — Back one character
CTRL w         — Back one word
CRTL u         — Back to beginning of insert

Move text from file old to file new
vi old “a10yy     — Yank 10 lines to buffer a
:w                 — Write work buffer
:enew         — Edit new file
“ap             — Put text from a after cursor

Deletion commands
dd                 — Delete line
ndd             — Delete n lines to general buffer
dw                 — Delete word to general buffer
ndw             — Delete n words
d)                 — Delete to end of sentence
db                 — Delete previous word
D                 — Delete to end of line
x                 — Delete character

Recovering deletions
p                 — Put general buffer after cursor
P                 — Put general buffer before cursor

Change commands
s                 — Substitute (ESC) – 1 char with string
cw                 — Change word (ESC)
cc                 — Change line (ESC) – blanks line
c$                 — Change to end of line
rc                 — Replace character with c
R/td>         — Replace (ESC) – typeover
.                 — Repeat last change

Undo commands
u                 — Undo last change
U                 — Undo all changes on line

Rearrangement commands
yy or Y     — Yank (copy) line to general buffer
“z6yy         — Yank 6 lines to buffer z
yw                 — Yank word to general buffer
“a9dd         — Delete 9 lines to buffer a
“A9dd         — Delete 9 lines; append to buffer a
“ap             — Put text from buffer a after cursor
p                 — Put general buffer after cursor
P                 — Put general buffer before cursor
J                 — Join lines

File management commands
:w name     — Write edit buffer to file name
:wq             — Write to file and quit
:q!             — Quit without saving changes
ZZ                 — Same as :wq
:sh             — Execute shell commands ( CTRL d)

Parameters
:set list             — Show invisible characters
:set nolist         — Don’t show invisible characters
:set number         — Show line numbers
:set nonumber     — Don’t show line numbers
:syn on / off     — Turn on / off syntax highlighting

Tricks
%s/string1/string2/g         — Global search and replace
%s/string1/string2/gc     — Global replace with confirm
^N or ^P                                 — Autocomplete from previously typed words

Read more at melecio.org

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s