home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the vi EditorSearch this book

2.3. Simple Edits

When you enter text in your file, it is rarely perfect. You find typos or want to improve on a phrase; sometimes your program has a bug. Once you enter text, you have to be able to change it, delete it, move it, or copy it. Figure 2.3 shows the kinds of edits you might want to make to a file. The edits are indicated by proofreading marks.

Figure 2.3

Figure 2.3. Proofreading edits

In vi you can perform any of these edits with a few basic keystrokes: i for insert (which you've already seen); a for append; c for change; and d for delete. To move or copy text, you use a pair of commands. You move text with a d for delete, then a p for put; you copy text with a y for "yank," then a p for put. Each type of edit is described in this section. Figure 2.4 shows the vi commands you use to make the edits marked in Figure 2.3.

Figure 2.4

Figure 2.4. Edits with vi commands

2.3.1. Inserting New Text

You have already seen the insert command used to enter text into a new file. You also use the insert command while editing existing text to add missing characters, words, and sentences. In the file practice, suppose you have the sentence:

Figure 2.3.1

with the cursor positioned as shown. To insert With a screen editor at the beginning of the sentence, enter the following:

Keystrokes Results
2k Figure

Move the cursor up two lines with the k command, to the line where you want to make the insertion.

iWith a Figure

Press i to enter insert mode and begin inserting text.

screen editorESC Figure

Finish inserting text, and press ESC to end the insert and return to command mode.

On the screen shown in the example above, vi pushes existing text to the right as the new text is inserted. That is because we are assuming that you are using vi on an "intelligent" terminal that can rewrite the screen with each character you type. An insert on a "dumb" terminal (such as an adm3a) will look different. The terminal itself cannot handle the overhead of updating the screen for each character typed (without a tremendous sacrifice of speed), so vi doesn't rewrite the screen until after you press ESC. On a dumb terminal, the same insert would appear:

Keystrokes Result
iWith a Figure

Press i to enter insert mode and begin inserting text. The dumb terminal appears to overwrite the existing text on the line.

screen editor Figure

The insertion appears to have overwritten existing text.

ESC Figure

After you have finished inserting text, press ESC to end the insert and return to command mode. vi now rewrites the line, so that you see all existing text.

2.3.3. Changing Text

Figure You can replace any text in your file with the change command, c. In order to tell c how much text to change, you combine c with a movement command. In this way, a movement command serves as a text object for the c command to affect. For example, c can be used to change text from the cursor:

cw
to the end of a word.

c2b
back two words

c$
to the end of line

c0
to the beginning of line.

After issuing a change command, you can replace the identified text with any amount of new text, with no characters at all, with one word, or with hundreds of lines. c, like i and a, leaves you in insert mode until you press the ESC key.

When the change only affects the current line, vi marks the end of the text that will be changed with a $, so that you can see what part of the line is affected. (See the example for cw, below.)

2.3.3.1. Words

Figure Figure To change a word, combine the c (change) command with w for word. You can replace a word (cw) with a longer or shorter word (or any amount of text). cw can be thought of as "delete the word marked and insert new text until ESC is pressed."

Suppose you have the following line in your file practice:

With an editor you can scroll the page,

and want to change an to a screen. You need to change only one word:

Keystrokes Results
w Figure

Move with w to the place you want the edit to begin.

cw Figure

Give the change word command. The end of the text to be changed will be marked with a $ (dollar sign).

a screen Figure

Type in the replacement text, and then press ESC to return to command mode.

cw also works on a portion of a word. For example, to change spelling to spelled, you can position the cursor on the i, type cw, then type ed, and finish with ESC.

General Form of vi Commands

In the change commands we've mentioned up to this point, you may have noticed the following pattern:

(command)(text object)

command is the change command c, and text object is a movement command (you don't type the parentheses). But c is not the only command that requires a text object. The d command (delete) and the y command (yank) follow this pattern as well.

Remember also that movement commands take numeric arguments, so numbers can be added to the text objects of c, d, and y commands. For example, d2w and 2dw are commands to delete two words. With this in mind, you can see that most vi commands follow a general pattern:

(command)(number)(text object)

or the equivalent form:

(number)(command)(text object)

Here's how this works. number and command are optional. Without them, you simply have a movement command. If you add a number, you have a multiple movement. On the other hand, combine a command (c, d, or y) with a text object to get an editing command.

When you realize how many combinations are possible in this way, vi becomes a powerful editor indeed!

2.3.5. Deleting Text

Figure You can also delete any text in your file with the delete command d. Like the change command, the delete command requires a text object (the amount of text to be operated on). You can delete by word (dw), by line (dd and D), or by other movement commands that you will learn later.

With all deletions, you move to where you want the edit to take place, then give the delete command (d) and the text object, such as w for word.

2.3.6. Moving Text

In vi, you move text by deleting it and then placing that deleted text elsewhere in the file, like a "cut and paste." Each time you delete a text block, that deletion is temporarily saved in a special buffer. Move to another position in your file and use the put command (p) to place that text in the new position. You can move any block of text, although moving is more useful with lines than with words.

Figure The put command (p) puts the text that is in the buffer after the cursor position. The uppercase version of the command, P, puts the text before the cursor. If you delete one or more lines, p puts the deleted text on a new line(s) below the cursor. If you delete less than an entire line, p puts the deleted text on the current line, after the cursor.

Suppose in your file practice you have the text:

Figure 2.3.6

and want to move the second line, like a "cut and paste", below the third line. Using delete, you can make this edit.

Keystrokes Results
dd Figure

With the cursor on the second line, delete that line. The text is placed in a buffer (reserved memory).

p Figure

Give the put command, p, to restore the deleted line at the next line below the cursor. To finish reordering this sentence, you would also have to change the capitalization and punctuation (with r) to match the new structure.

NOTE:

Once you delete text, you must restore it before the next change command or delete command. If you make another edit that affects the buffer, your deleted text will be lost. You can repeat the put over and over, so long as you don't make a new edit. In Chapter 4, you will learn how to save text you delete in a named buffer so you can retrieve it later.

2.3.7. Copying Text

Figure Often you can save editing time (and keystrokes) by copying a part of your file to use in other places. With the two commands y (for yank) and p (for put), you can copy any amount of text and put that copied text in another place in the file. A yank command copies the selected text into a special buffer, where it is held until another yank (or deletion) occurs. You can then place this copy elsewhere in the file with the put command.

As with change and delete, the yank command can be combined with any movement command (yw, y$, 4yy). Yank is most frequently used with a line (or more) of text, because to yank and put a word usually takes longer than simply to insert the word.

The shortcut yy operates on an entire line, just as dd and cc do. But the shortcut Y, for some reason, does not operate the way D and C do. Instead of yanking from the current position to the end of the line, Y yanks the whole line. Y does the same thing as yy.

Suppose you have in your file practice the text:

Figure 2.3.7

You want to make three complete sentences, beginning each with With a screen editor you can. Instead of moving through the file, making this edit over and over, you can use a yank and put to copy the text to be added.

Keystrokes Results
yy Figure

Yank the line of text that you want to copy into the buffer. The cursor can be anywhere on the line you want to yank (or on the first line of a series of lines).

2j Figure

Move the cursor to where you want to put the yanked text.

P Figure

Put the yanked text above the cursor line with P.

jp Figure

Move the cursor down a line and put the yanked text below the cursor line with p.

Yanking uses the same buffer as deleting. Each new deletion or yank replaces the previous contents of the yank buffer. As we'll see in Chapter 4, up to nine previous yanks or deletions can be recalled with put commands. You can also yank or delete directly into up to 26 named buffers, which allows you to juggle multiple text blocks at once.

2.3.8. Repeating or Undoing Your Last Command

Each edit command that you give is stored in a temporary buffer until you give the next command. For example, if you insert the after a word in your file, the command used to insert the text, along with the text that you entered, is temporarily saved.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.