Abbey Workshop

vi Cheat Sheet

Movement
h - left j - down k - up l - right
$ - end of line 0 - Beginning of line
w - forward one word b -- back one word e - end of the current word
Return -- Moves down to the beginning of the next line
^d - down half a screen ^u - up half a screen
^f -- Pages forward one screen ^b -- Pages back one screen
G -- Goes to the last line of the file 1G -- Goes to the first line of the file
:21 -- Goes to line 21 21G -- Goes to line 21

Insert and Delete
x - delete character X - delete previous character
dd - delete line dw - delete word D - delete to eol
i - insert at cursor a - append after cursor A - append at eol
o - open line below O - open line above
:5,10d -- Deletes lines 5 through 10

Searching
/string -- Searches forward for the string ?string -- Searches backward for the string
n -- Finds the next occurrence N -- Finds the previous occurrence
:%s/old/new/g -- Searches and replaces globally

Copy, Move, Manipulate
J - join next line
ayy -- Yanks a copy of line to buffer a
a7yy -- Yanks next 7 lines into buffer a
"ap -- Put the contents of buffer a after cursor
"aP -- Put the contents of buffer a before cursor
:1,3 co 5 -- Copies lines 1 through 3 and puts them after line 5
:4,6 m 8 -- Moves lines 4 through 6 to line 8 (line 6 becomes line 8, line 5 becomes line 7, and line 4 becomes line 6)
:r filename -- Inserts (reads) the file at line after the cursor

Saving and Quitting
:w -- Saves changes (write buffer)
:w new_filename -- Writes buffer to new_filename
:wq -- Saves changes and quits vi
:x -- Saves changes and quits vi
ZZ -- Saves changes and quits vi
:q! -- Quits without saving changes
:wq! -- Saves changes and quits vi (the ! overrides read-only permissions for the owner of the file only)

Set Command Options
:set nu -- Shows line numbers
:set nonu -- Hides line numbers
:set ic -- Instructs searches to ignore case
:set noic -- Instructs searches to be case-sensitive
:set list -- Displays invisible characters, such as tab and end-of-line
:set nolist -- Turns off the display of invisible characters
:set showmode -- Displays current mode of operation
:set noshowmode -- Turns off mode display
:set -- Displays all vi variables set
:set all -- Displays all possible vi variables and their current settings