Deleting Text

To remove a character from a string of text, press the x key. Compare Figures 11-4 and 11-5 for the results.

FIGURE 11-4

Before character removal

1.Z5 AI

You can see in the figures that the s in insert was removed. The x key in command mode can be thought of as a replacement for the Backspace key. You will find after repeated use of vi that you will not use the Backspace key at all. We have even used the x command in Word as we are in the mindset that we are editing text and we should use the x key to remove text. We hope that the editors of this book will spot any erroneous x's in the text!

Deleting More Than One Character at a Time

Often you want to remove whole lines of text, and vi enables you to do this very quickly with the d command.

The d command can be used to remove a whole line, a word, part of a word, multiple lines, and multiple words.

After character removal

1,30_nl

To remove a word of text (text surrounded by a space), move the cursor to the start of the word and press d+w sequentially. If you want to remove the part of a word, position the cursor at the character you want to remove to the end of the word and use the d+w command.

r ! i j It may be slightly confusing to put these commands into practice in your head, so we advise that you find a text file (or create your own) full of text and play around with the commands we discuss here.

To remove a full line of text, press d+d sequentially. The double d removes the whole line of text, until it finds the end of the line. It may be that you cannot see the entire text on the line if it is longer than your terminal display, so be careful when you remove a line.

To remove all text from the cursor position to the end of the current line, press d and then $.

Undoing and Redoing

The vim editor also features an undo command that proves very helpful. If you have made a mistake (for example, removing a line you didn't mean to), pressing u while in command mode will undo the last operation you performed. Pressing u again will undo the previous operation before this and so on. To redo an operation you have undone, press the Ctrl +r key (redo).

Removing Multiple Times

To remove lines or characters multiple times, you can specify a number to work with the previous commands. For example, to remove five lines of text, press 5+d+d. In Figure 11-6, you can see a series of lines before the five lines of text are removed. In Figure 11-7, the operation 5+d+d has been used to remove Lines 3 through 7.

FIGURE 11-6

Removing multiple lines of text (before)

This

is

line

i

This

IS

line

2

This

IS

1 i tie

3

This

IS

line

4

This

IS

1 i lie

5

This

IS

line

fa

This

is

1 i lie

?

This

IS

1 ine

U

This

IS

1 ine

■J

This

IS

1 ine

1U

You can use this operation to remove characters (number+x), lines (number+d+d).

Was this article helpful?

0 0

Post a comment