|
|
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:
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.
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''.