Basic Unix vi Editor Commands

Notes:
• In Unix it matters whether you type upper or lowercase letters.
• In many cases typing a number before a command causes that action to be repeated that number of times.
• Don't press the Enter/Return key unless instructed to do so.

Command to enter vi edit mode

vi filename (press Enter/Return key)
Opens the named file if it exists, or creates new file and opens it.

Commands to move the cursor

k    j    l    h (lowercase)
These keys move the cursor one line above, one line below, one space right, or one space left respectively.
w (lowercase)
Moves cursor to next word.
b (lowercase)
Moves cursor to last word.
arrow keys
Move cursor one space in direction of arrow.
0 (zero)
Moves cursor to the beginning of current line.
$ (dollar sign)
Moves cursor to the end of the current line
- (hyphen)
Moves cursor to the beginning of the previous line.
+ (plus sign)
Moves cursor to the beginning of the next line.
G (uppercase)
Moves cursor to the last line in the file.
1G (number one and uppercase G)
Moves cursor to the first line in the file.

Commands to move to another screen

Note: Hold down the Ctrl (control) key while pressing the letter indicated.

Ctrl-b (lowercase)
Moves back to the previous screen.
Ctrl-f (lowercase)
Moves forward to the next screen.
Ctrl-u (lowercase)
Moves up (back) one half screen.
Ctrl-d (lowercase)
Moves down (forward) one half screen.

Commands to put you in text insert mode

Note: After inserting text, press Esc (escape) key to return to command mode.

a (lowercase)
Use to insert (append) text to the right of cursor.
A (uppercase)
Use to insert (append) text at the end of the current line.
i (letter i (eye) lowercase)
Use to insert text to the left of cursor.
I (letter I (eye) uppercase)
Use to insert text at the beginning of the current line.
o (letter o lowercase)
Use to open a new line for inserting text below cursor line.
O (letter O uppercase)
Use to open a new line for inserting text above cursor line.

Commands to change text

Note: After making change, press Esc (escape) key to return to command mode.

cw (lowercase)
Changes word under and to right of cursor to new text you enter.
cc (lowercase)
Changes current line to new text you enter.
s (lowercase)
Substitutes character under cursor with new text you enter.
S (uppercase)
Substitutes current line with new text you enter.
C (uppercase)
Changes text from cursor to end of line to text you enter.
r (lowercase)
Replaces character under cursor with new character.
~ (tilde)
Changes case of character under cursor.

Commands to delete text

x (lowercase)
Removes the character under the cursor.
dw (lowercase)
Deletes word under and to right of cursor.
dd (lowercase)
Deletes the cursor line.
D (uppercase)
Deletes from cursor to end of line.

Command to join two lines

J (uppercase)
Joins next line to the end of the cursor line.

Commands to search for a word or string of words

/keyword or /string (press Enter/Return key)
Searches forward through text. Locates first instance of keyword or string.
?keyword or ?string (press Enter/Return key)
Searches backward through text. Locates first instance of keyword or string.
n (lowercase)
Searches in direction of original /keyword, ?keyword, /string, or ?string command. Locates next instance of keyword or string.
N (uppercase)
Searches in opposite direction of original /keyword, ?keyword, /string, or ?string command. Locates next instance of keyword or string.

Commands to copy lines and put them in another location

yw (lowercase)
Yanks (copies) word under and to right of cursor.
Y (uppercase)
Yanks (copies) line the cursor is on.
p (lowercase)
Puts last word or line yanked or deleted after cursor.
P (uppercase)
Puts last word or line yanked or deleted before cursor.

Command to recover from last change

u (lowercase)
Undoes last change.

Commands to save changes or leave vi editing session

:w (colon and lowercase w)
Saves changes, but does not leave vi editing session.
:wq (colon and lowercase wq)
Saves changes, and leaves vi editing session.
ZZ (uppercase)
Saves changes, and leaves vi editing session.
:q! (colon, lowercase q, and exclamation)
Does not save changes made. Just leaves vi editing session.

See Basic Unix directory commands for quick directory command instructions.