Chapter 16 Vi

vi(1) is the standard Unix text editing program, and while
mastering it is not as essential as it once was, is still a very rewarding goal. There
are several versions (or clones) of vi available, including vi, elvis, vile,
and vim. One of these is available on just about any version of
Unix, as well as on Linux. All of these versions include the same basic feature set and
commands, so learning one clone should make it easy to learn another. With the variety of
text editors included with Linux distributions and Unix variants these days, many people
no longer use vi. Still, it remains the most universal text
editor across Unix and Unix work-alikes. Mastering vi means you
should never be sitting at a Unix machine and not be comfortable with at least one
powerful text editor.

vi includes a number of powerful features including syntax
highlighting, code formatting, a powerful search-and-replace mechanism, macros, and more.
These features make it especially attractive to programmers, web developers, and the
like. System administrators will appreciate the automation and integration with the shell
that is possible.

On Slackware Linux, the default version of vi available is
elvis. Other versions - including vim
and gvim - are available if you've installed the proper
packages. gvim is an X Window version of vim that includes toolbars, detachable menus, and dialog boxes.

vi can be started from the command line in a variety of ways.
The simplest form is just:

%vi

Figure 16-1. A vi session.

This will start up vi with an empty buffer. At this point,
you'll see a mostly blank screen. It is now in “command mode”, waiting for
you to do something. For a discussion of the various vi modes,
see the Section 16.2. In order to quit out of vi, type the following:

:q

Assuming that there have been no changes to the file, this will cause vi to quit. If there have been changes made, it will warn you that
there have been changes and tell you how to disregard them. Disregarding changes usually
means appending an exclamation point after the “q” like
so:

:q!

The exclamation point usually means to force some action. We'll discuss it and other
key combinations in further details later.

You can also start vi with a pre-existing file. For example,
the file /etc/resolv.conf would be opened like so:

%vi /etc/resolv.conf

Finally, vi can be started on a particular line of a file.
This is especially useful for programmers when an error message includes the line their
program bombed on. For example, you could start up vi on line 47
of /usr/src/linux/init/main.c like so:

%vi +47 /usr/src/linux/init/main.c

vi will display the given file and will place the cursor at
the specified line. In the case where you specify a line that is after the end of the
file, vi will place the cursor on the last line. This is
especially helpful for programmers, as they can jump straight to the location in the file
that an error occurred, without having to search for it.