|< Day Day Up >|
This section describes the commands to delete and replace, or change, text in the document you are editing. The Undo command is covered here because it allows you to restore deleted or changed text.
The u command (Undo) restores text that you just deleted or changed by mistake. A single Undo command restores only the most recently deleted text. If you delete a line and then change a word, the first Undo restores only the changed word; you have to give a second Undo command to restore the deleted line. The U command restores the last line you changed to the way it was before you started changing it, even after several changes.
Delete character (x/X)
The x command deletes the current character. You can precede the x command by a Repeat Factor (page 187) to delete several characters on the current line, starting with the current character. The X command deletes characters to the left of the cursor.
The d (Delete) command removes text from the Work buffer. The amount of text that d removes depends on the Repeat Factor and the Unit of Measure (page 184) you enter after the d. After the text is deleted, vim is still in Command mode.
tip: Use dd to delete a single line
The command d RETURN deletes two lines: the current line and the following one. Use dd to delete just the current line, or precede dd by a Repeat Factor (page 187) to delete several lines.
You can delete from the current cursor position up to a specific character on the same line. To delete up to the next semicolon (;), give the command dt; (see page 164 for more information on the t command). To delete the remainder of the current line, use D or d$ . Table 6-1 lists some Delete commands. Each command, except the last group that starts with dd, deletes from/to the current character.
tip: Exchange characters and lines
If two characters are out of order, position the cursor on the first character and give the commands xp.
If two lines are out of order, position the cursor on the first line and give the commands ddp.
See page 172 for more information on the Put commands.
The c (Change) command replaces existing text with new text. The new text does not have to occupy the same space as the existing text. You can change a word to several words, a line to several lines, or a paragraph to a single character. The C command replaces the text from the cursor position to the end of the line.
The c command deletes the amount of text specified by the Repeat Factor and the Unit of Measure (page 184) and puts vim in Input mode. When you finish entering the new text and press ESCAPE, the old word, line, sentence, or paragraph is changed to the new one. Pressing ESCAPE without entering new text deletes the specified text (replaces the specified text with nothing).
Table 6-2 lists some Change commands. Except for the last two, each command changes text from/to the current character.
tip: dw works differently from cw
The dw command deletes all the characters through (including) the SPACE at the end of a word. The cw command changes only the characters in the word, leaving the trailing SPACE intact.
The s and S (Substitute) commands also replace existing text with new text (Table 6-3). The s command deletes the character the cursor is on and puts vim into Input mode. It has the effect of replacing the single character that the cursor is on with whatever you type until you press ESCAPE. The S command does the same thing as the cc command: It changes the current line. The s command replaces characters only on the current line. If you specify a Repeat Factor before an s command and this action would replace more characters than exist on the current line, s changes characters only to the end of the line (same as C).
The tilde (~) character changes the case of the character under the cursor from uppercase to lowercase, or vice versa. You can precede the tilde with a number to specify the number of characters you want the command to affect. For example, 5~ will transpose the next five characters starting with the character under the cursor, but it will not transpose characters past the end of the line the cursor is on.
|< Day Day Up >|