Running Linux, 5th Edition - Matthias Kalle Dalheimer [352]
Editing Files Using vi
This section covers the use of the vi (pronounced "vee-eye") text editor. vi was the first real screen-based editor for Unix systems. It is also simple, small, and sleek. If you're a system administrator, learning vi can be invaluable; in many cases, larger editors, such as (X)Emacs, won't be available in emergency situations (for instance, when booting Linux from a maintenance disk).
vi is based on the same principles as many other Unix applications: that each program provides a small, specific function and is able to interact with other programs. For example, vi doesn't include its own spell checker or paragraph filler, but those features are provided by other programs that are easy to fire off from within vi. Therefore, vi itself is a bit limited, but is able to interact with other applications to provide virtually any functionality you might want.
At first, vi may appear to be somewhat complex and unwieldy. However, its single-letter commands are fast and powerful once you've learned them. The next section describes Emacs, a more flexible editor (really an integrated work environment) with an easier learning curve. Do keep in mind that knowing vi may be essential to you if you are in a situation where (X)Emacs is not available, so we encourage you to learn the basics, as odd as they may seem. It should also be added that a number of vi clones are now available that are much more comfortable to use than the original vi, the most popular of which is vim (vi improved). Chances are that your distribution has things set up so that when starting vi, you actually start one of those. We stick to the basics here, though, so that you can use the information presented here no matter which version of vi you use. You can find coverage of the newer versions in the book Learning the vi Editor by Linda Lamb and Arnold Robbins (O'Reilly).
Starting vi
Let's fire up vi and edit a file. The syntax for vi is:
vi filename
For example:
eggplant$ vi test
will edit the file test. Your screen should look like Figure 19-1.
Figure 19-1. vi when opening a new file
The column of ~ characters indicates that you are at the end of the file.
Inserting Text and Moving Around
While using vi, at any one time you are in one of two (or three, depending on how you look at it) modes of operation. These modes are known as command mode, edit mode, and ex mode.
After starting vi, you are in command mode. This mode allows you to use a number of (usually single-letter) commands to modify text , as we'll see soon. Text is actually inserted and modified within edit mode. To begin inserting text, press i (which will place you into edit mode) and begin typing. See Figure 19-2.
Figure 19-2. Entering text into vi buffer
While inserting text, you may type as many lines as you wish (pressing the Enter key after each, of course), and you may correct mistakes using the Backspace key. To end edit mode and return to command mode, press the Esc key.
While in command mode, you can use the arrow keys to move around the file. Alternatively, or when the arrow keys don't work, you may use h, j, k, and l, which move the cursor left, down, up, and right, respectively.
There are several ways to insert text other than using the i command. The a command (for "append") inserts text after the current cursor position. For example, use the left arrow key to move the cursor between the words good and men (Figure 19-3).
Figure 19-3. Positioning cursor in vi
Press a, type wo, and then press Esc to return to command mode (Figure 19-4).
Figure 19-4. vi after insertion
To open a line below the current one and begin inserting text, use the o command. Press o and type another line or two (Figure 19-5).
Figure 19-5. vi after appending text
Remember that at any time you're either in command