Editing files

Deleting and restoring text

To delete text, use the d command followed by the unit of text to delete. To delete several units of text at once, enter the number of items to delete (n) followed by the deletion command. The deletion commands available are:

Delete letter under the cursor. 12x, for example, deletes 12 letters.

Delete letter to the left of the cursor.

Delete word from the cursor up to the next word including any white space. 4dw, for example, deletes four words.

Delete the current line. 8dd, for example, deletes eight lines.

Delete from cursor to the end of the file.

[n]:x,y d
Delete lines x through y of a file. For example, :100,200 d deletes lines 100 through 200, :1,. d deletes from the start of a file to the current line, and :.,$ d has the same effect as dG.

Restoring deleted text

You can always undo the last deletion by using the u or U (undo) command.

The command p (paste) inserts the last piece of text that you deleted to the right of the cursor. This is useful if you need to transpose two adjacent characters: position the cursor on the first character and enter xp. To swap two lines, place your cursor on the first, and enter ddp.

You can restore deleted text before the cursor by using the P command (uppercase) instead of p (lowercase). You can use this to swap two words that are on the same line; place the cursor on the first character of the first word, and enter dwwP.

Using the deletion buffers

vi remembers the last nine pieces of text that you deleted in deletion buffers numbered 1 for the most recent deletion, 2 for the next most recent, and so on. To restore the contents of a deletion buffer below the line on which the cursor is positioned, use the paste command with the number of the deletion buffer, "numberp. For example, typing "2p pastes the second most recently deleted piece of text at the cursor.

If you switch to editing another file (using :n or :r), or reload the original file (using :rew!), the contents of the deletion buffers are preserved so that you can cut and paste between files. The contents of all buffers are lost, however, when you quit vi.

There are also 26 named text buffers that you can use in vi, these are described in ``Using buffers to cut and paste text''.

Next topic: Searching for text
Previous topic: Moving around a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003