Keystrokes
Results
2dd
changes as you read through a file.
Give the command to delete two lines (2dd). Note that even though the cursor was not posi-
tioned on the beginning of the line, the entire line is deleted.
The D command deletes from the cursor position to the end of the line. (D is a
D shortcut for d$.) For example, with the cursor positioned as shown:
Screen editors are very popular,
since they allow you to make
changes as you read through a file.
24 | Chapter 2:Simple Editing
you can delete the portion of the line to the right of the cursor:
Keystrokes
Results
D
Screen editors are very popular,
since they allow you to make
changes
Give the command to delete the portion of the line to the right of the cursor (D).
Characters
Often you want to delete only one or two characters. Just as r is a special change
x command to replace a single character, x is a special delete command to delete a
single character. x deletes only the character the cursor is on. In the line here:
zYou can move text by deleting text and then
you can delete the letter z by pressing x. A capital X deletes the character before the
cursor. Prefix either of these commands with a number to delete that number of char-
acters. For example, 5x will delete the five characters under and to the right of the cursor.
Problems with deletions
Youve deleted the wrong text and you want to get it back.
There are several ways to recover deleted text. If youve just deleted something and
you realize you want it back, simply type u to undo the last command (for example,
a dd). This works only if you havent given any further commands, since u undoes
only the most recent command. Alternatively, a U will restore the line to its pristine
state, the way it was before any changes were applied to it.
You can still recover a recent deletion, however, by using the p command, since
vi saves the last nine deletions in nine numbered deletion buffers. If you know, for
example, that the third deletion back is the one you want to restore, type:
"3p
to put the contents of buffer number 3 on the line below the cursor.
This works only for a deleted line . Words, or a portion of a line, are not
saved in a
buffer. If you want to restore a deleted word or line fragment, and u wont work,
use the p command by itself. This restores whatever youve last deleted. The next
few subsections will talk more about the commands u and p.
Note that Vim supports infinite undo, which makes life much easier. See the
section Undoing Undos on page 296 for more information.
The mnemonic for x is that it is supposedly like x-ing out mistakes with a typewriter. Of course, who uses
a typewriter anymore?
Simple Edits | 25
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.
The put command (p) puts the text that is in the buffer after the cursor position.
p 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 into the current line, after the
cursor.
Suppose in your file practice you have the text:
You can move text by deleting it and then,
like a "cut and paste,"
placing the deleted text elsewhere in the file.
each time you delete a text block.
and you 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
You can move text by deleting it and then,
placing the deleted text elsewhere in the file.
each time you delete a text block.
With the cursor on the second line, delete that line. The text is placed in a buffer (reserved
memory).
p
You can 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.
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.
Once you delete text, you must restore it before the next change com-
mand or delete command. If you make another edit that affects the buf-
fer, your deleted text will be lost. You can repeat the put over and over,
so long as you dont make a new edit. In Chapter 4, you will learn how
to save text you delete in a named buffer so that you can retrieve it later.
Transposing two letters
You can use xp (delete character and put after cursor) to transpose two letters. For
example, in the word mvoe , the letters vo are transposed (reversed). To correct a