< Previous | Contents | Next >
Deleting Text
As we might expect, vi offers a variety of ways to delete text, all of which contain one of two keystrokes. First, the x key will delete a character at the cursor location. x may be preceded by a number specifying how many characters are to be deleted. The d key is more general purpose. Like x, it may be preceded by a number specifying the number of times the deletion is to be performed. In addition, d is always followed by a movement command that controls the size of the deletion. Here are some examples:
Table 12-3: Text Deletion Commands
Command Deletes
Command Deletes
x The current character.
3x The current character and the next two characters.
dd The current line.
5dd The current line and the next four lines.
dW From the current cursor position to the beginning of the next word.
d$ From the current cursor location to the end of the current line.
d0 From the current cursor location to the beginning of the line.
d^ From the current cursor location to the first non- whitespace character in the line.
dG From the current line to the end of the file.
d20G From the current line to the twentieth line of the file.
Place the cursor on the word “It” on the first line of our text. Press the x key repeatedly until the rest of the sentence is deleted. Next, press the u key repeatedly until the deletion
is undone.
Note: Real vi only supports a single level of undo. vim supports multiple levels.
Let's try the deletion again, this time using the d command. Again, move the cursor to the word “It” and press dW to delete the word:
The quick brown fox jumped over the lazy dog. was cool. Line 2
Line 3
Line 4
Line 5
The quick brown fox jumped over the lazy dog. was cool. Line 2
Line 3
Line 4
Line 5
Press d$ to delete from the cursor position to the end of the line:
The quick brown fox jumped over the lazy dog. Line 2
Line 3
Line 4
Line 5
The quick brown fox jumped over the lazy dog. Line 2
Line 3
Line 4
Line 5
Press dG to delete from the current line to the end of the file:
~
~
~
~
~
~
~
~
~
~
Press u three times to undo the deletion.