vi Commands

vi can be very difficult to use at first. Here is a reference of some of the useful commands for working with vi. This page is written using vim specifically, so some behavior might be slightly different when using vi.

Essential Commands
More Commands
Vim-Specific Commands

Essential Commands

These commands are the bare essentials necessary for vi to be a useable text editor.

i   - insert text before the cursor
o   - insert a new line below the current line
ESC - exit insert mode
x   - delete the current character
ZZ  - save and quit
:q! - quit without saving

Hitting either o or i will bring you into insert mode. All keys are interpreted literally in this mode. You must exit insert mode back to command mode in order to issue more commands.

More Commands

This is a more thorough but far from complete list of vi commands.

A number preceding any of these navigation, text deletion, or text modification commands issues it that many times. For instance, 5w skips 5 words.

Navigation:

h - left
j - down
k - up
l - right
w - next word
$ - end of line
0 - beginning of line

Used alone, navigation commands will direct the cursor. Used in conjuction with other commands, they set the direction of the command. Commands where this is possible will be written with a * as a placeholder for a navigation command.

Text Insertion:

i - insert text before the cursor
I - insert text at the beginning of the line
a - insert text after the cursor
A - insert text at the end of the line
o - insert a new line below the current line
O - insert a new line about the current line

Text Modification:

r  - replace the current character
R  - replace starting with the current character
cc - change the current line
c* - change in the direction specified by *

Text Deletion:

x  - delete the current character
dw - delete the current line
d* - delete in the direction specified by *

Copy and Paste:

Y  - copy the current line
yy - copy the current line
y* - copy in the direction specified by *
p  - paste the buffer below the current line
P  - paste the buffer above the current line

Note that text deletion commands also fill up vi's buffer, and may be pasted using 'p' and 'P'.

Miscellaneous:

u   - undo
ESC - exit insert mode
:w  - save
:w! - save in a read only file (may need to be root)
ZZ  - save and quit
:q! - quit without saving
/X - search forward for X
?X - search backward for X

After searching, you may use 'n' to skip to the next result.

Vim-Specific Commands

Vim (Vi improved) has some nice improvements (surprisingly) over vi. Syntax highlighting in particular is one of them. It also has some nice features in "Visual" mode.

. - repeat last command
v - enter visual mode
x - cut highlighted text (from visual mode)
y - copy hightlighted text (from visual mode)

If you hit '.' it will repeat your last command. For instance, type 'ixaenn' 'ESC' (insert the text xaenn). Then hit '.'. Enjoy!

As you can see by hitting 'v' you will enter visual mode. Now you can use the navigation keys (hjklw$) and the arrow keys to highlight text. Hit either x or y to add the highlighted text to the buffer. Now you can paste as you would normally. This is nice for getting fine-grained segments of text.