Getting Started with vi
Vi is an editor, a program that allows you to create and change files in your
computer account. You will use it to create and change your C programs, look
at output files, and any other task that requires inspecting the contents of
a file.
This guide provides the minimum amount of information to get you started using
vi. It leaves out many handy shortcuts, which are explained elsewhere, notably
in the on-line help system.
To create a file using vi, or to edit (change or view) one that already
exists, simply put the file name after the command "vi":
% vi program1.c
The percent sign is the UNIX prompt. You do not type it in. Always press
RETURN after typing in a UNIX command.
Now you will see your file on the screen. Information messages are presented
at the bottom of the screen.
There are two "modes" while you are using "vi": command mode and insert mode.
You start in command mode, but you insert text while in insert mode. {The
reason there are two modes is that vi was written in a day when terminals were
small and dumb, and didn't have function keys. Thus, all the letters on the
keyboard have double meanings -- as the ordinary letters they represent during
insert mode, and as special vi commands when in command mode.}
To enter text into the file, move the cursor to the spot where you wish to
start and then press "i". The letter "i" will not show up, but it will put
you into insert mode. Type in your text, using the backspace key to go back
over a mistake and fix it. If you are at the end of the line and wish to add
a new one, simply press RETURN. To get out of insert mode, press the ESCape
key once.
Generally, you can press ESC (the escape key) anytime you need to make sure
that you are in command mode. It will beep at you if you were already in
command mode, but it will not hurt anything.
Moving the cursor can be done easily by pressing the four arrow keys. However,
if you are in insert mode, pressing the arrow keys does strange things!
To quit vi and save the changes, make sure you are in command mode (by pressing
ESC). Then press capital Z twice! If you wish to quit, but you are unhappy
with the changes you made and wish the computer to ignore then, you must press
the colon, then type "q" (lowercase), then an exclamation mark!
:q!
and you will return to UNIX without altering the file.
About the only other basic information you need to know is how to remove
text from your file. To remove one character, position the cursor on top of
it, and press "x" (lowercase) while in command mode. To remove one whole
line, put the cursor anywhere on the line, and press "dd" (two lowercase d's
in a row.) Remember that these are commands and must be done in command mode.
To get to command mode, press ESC.
If you ever get into trouble, try pressing ESC, then holding down CONTROL,
press "l" (lowercase ell key). If this doesn't fix things, try pressing
ESC, then typing :q! to quit without saving changes. Finally, PANIC! No,
seek help from another student in the class, a Computer Science major, the
professor or the consultant.