A Brief Overview of and Introduction to VI


Jeff Offutt


VIsual editor

VI is the standard UNIX full screen editor. This document describes a very small set of VI commands - nowhere near the full power of VI, but enough to do most operations. VI is an extremely powerful and equally confusing editor. Very few people know all of its capabilities, although people who can use most of its capabilities find it a very good editor, particularly for programming. The way I learned was to start with a small set then expand my knowledge slowly through the help guide and by asking questions. The associated quick reference sheet contains all (the documented) VI commands.

1   VI Modes

VI is always in one of three states, or modes; command, insert or last-line.

Command
This is the initial mode. In command mode, every key is a command. The only error message VI gives you is a beep. If you get a beep, that means you have not goofed anything up. If you are unsure what mode you are in, type < esc > - that will always return you to command mode.
Insert
This is the mode in which you add text to your file. Insert is entered by a variety of commands, a couple of which are described below. Pressing the escape key ( < esc > ) will end insert mode.
Last-Line
Typing a ``:'' (colon) will place you into last-line mode. The cursor will hop to the bottom of your screen and you can enter various commands. The commands correspond to the line editor ``ed''. An escape or carrige return will end last-line mode.

2  Commands

Thirteen basic commands that are enough to perform most editing are described here. Your efficiency in using VI will increase dramatically if you learn new operations as soon as you are comfortable with these. Enter VI by typing ``vi filename''.

Remember that no carriage return is needed to enter a command when you're in command mode.

2.1  File Manipulation

:w
This last line command writes your text back to the file. Note the ``:'', which makes it a last line command. If you supply a file name, VI will write to that file, else it will write to the file that was supplied when you called VI.
:q
Quits the editor. Note the ``:'', which makes it a last line command. If you have made changes, VI will tell you so and inform you that the command ``:q!'' will quit anyway.

2.2  Positioning On Your File

Note that the four character positioning keys are found next to each other. The way to use VI is to put your right hand down with your four fingers covering the keys ``hjkl'' and learn to instinctively press the correct finger to move in the direction you want to go.
^F
Note that ``^F'' means < ctrl > -F. This moves you one entire screenfull forward through the file.
^B
Note that ``^B'' means < ctrl > -B. This moves you one entire screenfull backwards through the file.
j
Moves your cursor one line down on your screen.
k
Moves your cursor one line up on your screen.
h
Moves your cursor one character to the left on your screen.
l
Moves your cursor one character to the right on your screen.
CR
(carriage return) Moves your cursor to the beginning of the next line.
:1
Moves the cursor to the first line in the file.
:$
Moves the cursor to the last line in the file.

2.3  Operations on Text

i
Insert before the character. This places you into insert mode before the current position. < esc > will return you to command mode.
o
Open a new line below the current line. Once the line is created, you are in insert mode.
d
Delete. This key is used in conjunction with another key to specify how much to delete. ``dd'' deletes a line, ``dw'' deletes a word, and ``d$'' deletes to the end of the current line.
x
Delete one character.
u
The most important command! ``u'' undos the most recent command when you goof up.

Copyright Jeff Offutt, 1995