space, generating an interrupt signal, and other commands useful on serial terminals,
such as suspending and resuming output. These facilities were (and still are) managed
with the stty command.
The original UCB version of vi abstracted out the terminal control information from
the code (which was hard to change) into a text-file database of terminal capabilities
(which was easy to change), managed by the termcap library. In the early 1980s, Sys-
tem V introduced a binary terminal information database and terminfo library. The
two libraries were largely functionally equivalent. In order to tell vi which terminal you
had, you had to set the TERM environment variable. This was typically done in a shell
startup file, such as .profile or .login.
Today, everyone uses terminal emulators in a graphic environment (such as xterm). The
system almost always takes care of setting TERM for you. (You can use vi from a PC non-
GUI console too, of course. This is very useful when doing system recovery work in
single-user mode. There arent too many people left who would want to work this way
on a regular basis, though.) For day-to-day use, it is likely that you will want to use a
GUI version of vi, such as Vim or one of the other clones. On a Microsoft Windows
or Mac OS X system, this will probably be the default. However, when you run vi (or
some other screen editor of the same vintage) inside a terminal emulator, it still uses
TERM and termcap or terminfo and pays attention to the stty settings. And using it inside
a terminal emulator is just as easy a way to learn vi as any other.
Another important fact to understand about vi is that it was developed at a time when
Unix systems were considerably less stable than they are today. The vi user of yesteryear
had to be prepared for the system to crash at arbitrary times, and so vi included support
for recovering files that were in the middle of being edited when the system crashed.
So, as you learn vi and see the descriptions of various problems that might occur, bear
these historical developments in mind.
Opening and Closing Files
You can use vi to edit any text file. vi copies the file to be edited into a buffer (an area
temporarily set aside in memory), displays the buffer (though you can see only one
screenful at a time), and lets you add, delete, and change text. When you save your
edits, vi copies the edited buffer back into a permanent file, replacing the old file of the
same name. Remember that you are always working on a copy of your file in the buffer,
and that your edits will not affect your original file until you save the buffer. Saving
your edits is also called writing the buffer, or more commonly, writing your file.
Thankfully, this kind of thing is much less common, although systems can still crash due to external
circumstances, such as a power outage.
6 | Chapter 1:The vi Text Editor
Opening a File
vi is the Unix command that invokes the vi editor for an existing file or for
v
i
a brand new file. The syntax for the vi command is:
$ vi [ filename ]
The brackets shown on the above command line indicate that the filename is optional.
The brackets should not be typed. The $ is the Unix prompt. If the filename is omitted,
vi will open an unnamed buffer. You can assign the name when you write the buffer
into a file. For right now, though, lets stick to naming the file on the command line.
A filename must be unique inside its directory. A filename can include any
8-bit char-
acter except a slash (/), which is reserved as the separator between files and directories
in a pathname, and ASCII NUL, the character with all zero bits. You can even include
spaces in a filename by typing a backslash (\) before the space. In practice, though,
filenames generally consist of any combination of uppercase and lowercase letters,
numbers, and the characters dot (.) and underscore (_). Remember that Unix is case-
sensitive: lowercase letters are distinct from uppercase letters. Also remember that you
must press ENTER to tell Unix that you are finished issuing your command.
When you want to open a new file in a directory, give a new filename with the vi
command. For example, if you want to open a new file called practice in the current
directory, you would enter:
$ vi practice
Since this is a new file, the buffer is empty and the screen appears as follows:
~~~"practice" [New file]
The tildes (~) down the lefthand column of the screen indicate that there is no text in
the file, not even blank lines. The prompt line (also called the status line) at the bottom
of the screen echoes the name and status of the file.
You can also edit any existing text file in a directory by specifying its filename. Suppose
that there is a Unix file with the pathname /home/john/letter. If you are already in
the /home/john directory, use the relative pathname. For example:
$ vi letter
brings a copy of the file letter to the screen.
If you are in another directory, give the full pathname to begin editing: