Introduction To Vi: Dr. Mike Murphy
Introduction To Vi: Dr. Mike Murphy
Overview
The purpose of this tutorial is to:
Introduce the vi editor
Demonstrate a workflow for programming tasks using vi
Background
The vi editor, which stands for visual (by 1976 standards), is a command-line application for
editing text files. This editor predates such features as graphical interfaces, F-keys at the top of
the keyboard, and even arrow keys. It is useful to know how to use vi because it is the one
editor that is almost always installed on Unix systems, even when those systems lack a lot of
basic programs and a graphical interface. Technically speaking, Ubuntu actually supplies an
editor called vim, for vi improved, but the difference doesnt matter too much for our purposes.
This tutorial introduces the editor by demonstrating how to write a Hello, World application in
C, explaining both the basic use of the editor and a simple workflow for programming using the
editor in one Terminal window while compiling in a second Terminal window.
Procedure
1. Prepare a directory to hold a C programming project, and open a second Terminal
window. Arrange the two windows so that you can switch back and forth between them
easily.
2. Were going to create a file named hello.c to hold our basic Hello, World program. An
easy way to create a new, empty file is to use the touch command in one of the
Terminal windows. Run:
touch hello.c
3. Vi is started from the command line, taking the file to edit as an argument. To open vi on
our hello.c file, type the following:
vi h
Now press the Tab key on the keyboard. The CLI will automatically complete the
command for us, saving us some time. You should now have:
vi hello.c
4. Press Enter to start the editor.
5. The key to understanding vi is to realize that its designers had to do something clever to
support keyboards that only had a minimum set of keys. To enable files to be edited,
while at the same time providing a way to search, save, close the editor, etc., vi uses two
different modes: command mode and i nsert mode. The editor starts in command mode,
which upon first opening a file shows the file name and total number of lines and
characters in the lower-left portion of the screen (see above). In this case, our file is
empty, so the cursor appears at the top of the file, and lines below the cursor have the ~
character, which simply helps to show where the file ends.
6. To add some program code to our file, we need to change from command mode to insert
mode. To do this, press the i key on the keyboard (i for insert). You will now see the
word INSERT in the lower-left corner (be warned now that not every version of vi has
this... sometimes you just have to know what mode youre in).
7. Now we can type in some code. Carefully type the following, noting that in general, you
can only use backspace on the current line youre editing (this version of vim is actually a
bit more forgiving and will let you backspace between lines). This code example contains
an intentional syntax error.
#include <stdio.h>
You should wind up with something that looks like the following:
8. Now we need to save the file. First, switch back to command mode by hitting the Esc
(Escape) key in the upper left corner of the keyboard. Then, type the following special
command and hit the Enter key to save the file:
:w
That is a colon (full colon, not a semicolon - press shift along with the semicolon key)
followed by the letter w. The w here stands for write -- namely, to write the file to disk.
This version of vi will confirm the file has been written with a message in the lower left
corner of the window.
9. To compile the code, switch to the other Terminal window and run:
This means that the error is probably somewhere around line 5 of the file hello.c. I say
probably because there are some errors where the compiler will give an incorrect line
number.
To go to line 5 of the file, switch back to the Terminal window where vi is running. You
should still be in command mode. Type the following, and press the Enter key, to move
the cursor to line 5:
:5
11. In this case, the syntax error is actually a missing semicolon at the end of line 4. You can
move to line 4 either with the arrow keys or with the command : 4
13. Once you are finished editing the source file, exit with the command :q