Выбрать главу

Fedora Core, like most other Linux and Unix systems, stores most of its configuration information in text files. These files can be edited using various system administration tools, but they can also be edited by hand using any standard text editor.

vi is one such text editor. Some people love it, and some people hate it, but it has one advantage over just about every other editor available: it's universal. If you know how to use vi , you can confidently walk up to just about any Linux or Unix computer in the world and edit text files, so it's a valuable skill. The other nice fact about Vi is that it's not very demanding; you can use it in character mode or graphic mode, over a congested remote connection or with a foreign keyboard, and still get the job done. You can get by with less than a dozen commands to start, and then learn more when you need them.

vi is pronounced "vee-eye," not "vye" or "six."

4.4.1. How Do I Do That?

To start up the vi editor, simply type its name at a shell prompt, optionally providing the name of a file you wish to edit as an argument:

$ vi filename

The screen will clear, and the specified file will be displayed, as shown in Figure 4-4 .

Figure 4-4. Initial vi display

Notice that unused lines are marked with a tilde (~) character.

4.4.1.1. vi modes

vi uses two distinct modes:

Normal mode , where the text keys issue editing commands. This is sometimes called command mode .

Insert mode , where text keys insert text into the document.

The lower-left corner of the display shows the current mode: if it says -- INSERT -- , then you're in insert mode; otherwise, you're in normal mode.

4.4.1.2. Moving around

You can move the cursor around using the arrow keys. If your arrow keys don't work (which may be the case if you're using a remote connection from a bad terminal program), you can use the h, j, k, and l keys, as shown in Table 4-7 .

Table 4-7. Basic vi movement commands

Command Description
Left, h, or Backspace Move left one character.
Down or j Move down one line.
Up or k Move up one line.
Right, l, or Space Move right one character.
Enter Move to the start of the next line.
Home, ^, |, or 0 (Zero) Move to the start of the line.
End, $ Move to the end of the line.
:number Enter Move to line number.
:0 Enter Move to the start of the file.
:$ Move to the end of the file.
w Move forward one word.

You can put a number in front of any command to repeat the command. For example, typing 10j will move down 10 lines.

4.4.1.3. Inserting text

There are several commands for inserting text, as shown in Table 4-8 .

Table 4-8. Commands to enter insert mode

Command Description
i Insert before the cursor.
I Insert at the start of the line.
a Append after the cursor.
A Append at the end of the line.
o Open a line after the current line and insert text.
O Open a line before the current line and insert text.

All of these commands place the editor into insert mode; the only difference is where the cursor is positioned for the inserted text. The word -- INSERT -- will appear in the lower-left corner of the display.

To exit from insert mode and return to normal mode, press Esc. The -- INSERT -- indicator in the lower-left corner of the display will disappear.

4.4.1.4. Deleting, yanking, and putting: vi's version of cutting, copying, and pasting

vi offers three basic commands for deleting or yanking, as shown in Figure 4-9 . Deleting is roughly equivalent to cutting in a GUI-based editor, and yanking is similar to copying.

Table 4-9. Basic delete and yank commands

Command Description Examples
x Delete one character to the right of the cursor. x deletes one character to the right of the cursor; 25x deletes the character at the cursor position and 24 characters to the right.
X Delete one character to the left of the cursor. X deletes one character to the left of the cursor; 19X deletes 19 characters to the left.
d, followed by a cursor movement Delete from the cursor position to the indicated position. dj deletes the current line and the line below; dw deletes one word.
dd Deletes a line. dd deletes the current line; 15dd deletes 15 lines.
y, followed by a cursor movement Yank from the cursor position to the indicated position. yj yanks the current line and the line below; yw yanks one word.
yy Yanks a line. yy yanks the current line; 15yy yanks 15 lines.
p Puts yanked or deleted text after the cursor. If the text contains any partial lines, it is inserted directly after the cursor; otherwise, it is inserted starting on the next line. p puts one copy of the yanked text into the document after the cursor; 20p puts 20 copies of the yanked text after the cursor.
P Puts yanked or deleted text before the cursor. If the text contains any partial lines, it is inserted directly before the cursor; otherwise, it is inserted on the previous line. P puts one copy of the yanked text into the document before the cursor; 20P puts 20 copies of the yanked text before the cursor.