Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [353]

By Root 1447 0
mode (where commands such as i, a, or o are valid) or in edit mode (where you're inserting text, followed by Esc to return to command mode). If you're not sure which mode you're in, press Esc. This takes you out of edit mode, if you are in it, and does nothing except beep if you're already in command mode.

Deleting Text and Undoing Changes

From command mode, the x command deletes the character under the cursor. If you press x five times in our example, you end up with the screen shown in Figure 19-6.

Figure 19-6. vi after removing text

Now press a and insert some text, followed by Esc (Figure 19-7).

Figure 19-7. vi with new text

You can delete entire lines using the command dd (that is, press d twice in a row). If your cursor is on the second line in our example, dd will produce the screen shown in Figure 19-8.

Figure 19-8. vi after deleting lines

Text that is deleted may be reinserted using the p command (for "put"). Pressing p now will return the deleted line to the buffer after the current line. Using P (uppercase) instead will insert the text before the current line. By default, p and P insert text from the "undo buffer"; you can also yank and replace text from other buffers, as we'll see later.

The u command undoes the latest change (in this case, pressing u after dd is equivalent to p). If you inserted a large amount of text using the i command, pressing u immediately after returning to command mode would undo it.

To delete the word beneath the cursor, use the dw command. Place the cursor on the word Diet and type dw (see Figure 19-9).

Figure 19-9. vi after deleting a word

Changing Text

You can replace text using the R command, which overwrites the text beginning at the cursor. Place the cursor on the first letter in pizza, press R, and type (Figure 19-10).

The r command replaces the single character under the cursor. r does not place you in insert mode per se, so there is no reason to use Esc to return to command mode.

The ~ command changes the case of the letter under the cursor from upper- to lowercase, and vice versa. If you place the cursor on the o in Now in the previous example, and repeatedly press ~, you end up with the screen shown in Figure 19-11.

Another useful command for changing words is the cw command, which lets you simply type in the new word and—after pressing Esc—removes anything that might

Figure 19-10. vi after replacing text

Figure 19-11. Changing case in vi

be left over from the original word. If the new text is longer than the one being changed, the space is automatically expanded as needed.

Moving Around the File

You already know how to use the arrow keys to move around the document. In addition, the w command moves the cursor to the beginning of the next word, and b moves it to the beginning of the current word. The 0 (that's a zero) command moves the cursor to the beginning of the current line, and the $ command moves it to the end of the line.

When editing large files, you'll want to move forward or backward through the file one screen at a time. Pressing Ctrl-F moves the cursor one screen forward, and Ctrl-B moves it one screen backward.

To move the cursor to the end of the file, type G. You can also move to an arbitrary line: the command 10G would move the cursor to line 10 in the file. To move to the beginning of the file, use 1G.

Typing / followed by a pattern and the Enter key causes you to jump to the first occurrence of that pattern in the text following the cursor. For example, placing the cursor on the first line of text in our example and typing /burg moves the cursor to the beginning of the word "burgers." Using ? instead of / searches backward through the file.

The pattern following a / or ? command is actually a regular expression. Regular expressions are a powerful way to specify patterns for search and replace operations and are used by many Unix utilities. You can find more information about regular expressions in the section "Regular Expressions," later in this chapter. Using regular expressions,

Return Main Page Previous Page Next Page

®Online Book Reader