command mode

In command mode, keystrokes perform functions such as moving the cursor, searching for patterns, or quitting from the document. All commands are referenced from the current cursor position.

Cursor movement

Arrow Keys Move one space in any direction.
G Go to the last line in the file.
nG Go to line n.
w Move forward to the next word.
b Move backwards to the previous word.
$ Move to the end of the line.
0 Move to the beginning of the line.
CTRL+D Scroll down 1/2 screen.
CTRL+U Scroll up 1/2 screen.
CTRL+B Scroll up Full.
CTRL+F Scroll down Full.


/string Search for a "string" (pattern) of characters.
n Search for the next occurrence of the "string".
:%s/str1/str2/g Replace all occurrences of str1 with str2.

Deleting text

x Delete a single character.
dw Delete a word.
dd Delete an entire line.
ndd Delete an n number of lines.
d$ or D Delete from the cursor to the end of the line.

Copying text

yy Copy (yank) a line to the buffer.
nyy Copy (yank) an n number of lines to the buffer.
P Paste text from the buffer.

Changing text

r Mark a single character for replacement.
cw Mark a word for changing.
cc Mark a line for changing.

Miscellaneous commands

j Join the line below it.
!cmd Execute a UNIX command.
:r file Read a file into vi.
. Repeat the last command.
u Undo the last command.

Saving and exiting

u Write (save) the file.
:q! Quit the file without saving changes.
ZZ Write (save) the file and quit vi.