Unix Vi Guide

vi is a Visual Editor, which is where vi gets its name. Visual editors are ones that let you see the document that you are editing as you edit it. Almost all editors used today are visual editors, such as Microsoft Word and pico. Examples of non-visual editors are sed, ex, ed, and edlin (the last one being the editor shipped with DOS until relatively recently.) vi was written by William Joy as part of the bsd distribution of Unix. It was later used by AT&T, and has been standard Unix since.

The vi editor has two main modes of editing, Command Mode and Insert Mode. The command mode is used to issue commands to vi that let you edit the text, search and replace, and get into insert mode, to name a few functions. Once you are in insert mode, you can type all the text that you want. When you are finished with what you are inserting, press the ESC key to take you back to the command mode.

To invoke vi, simply type vi filename to open the vi editor with your filename in it. If the filename does not exist, vi will create a new file when you save your changes. You can also simply type vi and begin creating your file. You can then use the commands below to edit and save your file as needed.

Below is a list of some basic commands that can be used when you are in command mode. This list is meant as a quick reference and not as an detailed description of what these commands do. As always, the best way to learn something is to practice it.

a Insert text after the cursor.
i Insert text before the cursor.
o Open a new line below the line your cursor is currently on.
A Insert text at the end of the line.
I Insert text at the beginning of the line.
O Open a new line above the line your cursor is currently on.
x Delete the character beneath the cursor.
h Move your cursor to the left by one character.
l Move your cursor to the right by one character.
j Move your cursor up by one line.
k Move your cursor down by one line.
$ Move to the end of the line.
0 Move to the beginning of the line.
w Move forward one word and place the cursor on the first character of that word.
b Move backward one word and place the cursor on the first character of that word.
G Go to the end of the file.
1G Go to line 1 of the file.
CTRL+g Report on the current line number that the cursor if on as well as the number of total lines in the file.
dd Delete the current line.
cw Change word. That is, change the characters from where your cursor is to the next space or punctuation.
cl Change letter. Change the character that your cursor is on.
yy Copy the line that the cursor if currently on.
p Place the contents of either a deleted line or a copied line on the line immediately following the one your cursor is currently on.
u Undo the last change.
:w filename Save changes. The filename is optional and is only used to specify the name of the file that you would like to save your work as.
:wq filename Save changes and quit. Again, the filename is optional.
ZZ Save changes and quit.
:q! Quit without saving changes.
/string Search for next occurrence of string in the file.
n Repeat the last search
. Repeat the last insert function.

Test Yourself

If you feel up to seeing how much of this you retained, then take our VI Quiz.