vi(1) is the standard Unix text editing program, and mastering it is essential for system administrators. 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.
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 15-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 called Modes. 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.
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. 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.