Getting Rid of Stuff


You can get rid of the character under the cursor by pressing x.

If you want to press it 10 times, you can save effort by typing 10x.

It can be pretty handy, but you could very quickly get tired of counting how many times you want to press x, or you could get tired of holding down the x key. I know I would.

The more flexible delete command is very simple. It is the letter 'd' for "delete". It is one of the lucky mnemonic commands.

D will take a movement command.

You should have learned several in the earlier paragraph. The basic concept is that you will delete from the character under the cursor to some other point in the file.


You can delete the current line by typing "dd", or you can delete the current line and the one under it by typing d followed by the 'j' or 'down arrow'. Likewise, d followed by the } command (end of paragraph) deletes to the end of the current paragraph. d followed by G will delete to the end of the file. You'll find that all commands that take a movement command will work this way (including 'c').

Every movement command you learn increases your power to copy, delete, and retype. This added power is why it is essential that you learn to move well in VIM.
'd' will also take a repeat count, so you can type 23dd to delete 23 lines starting with the current line.

This can be handy.

Finally, we have registers. A VIM register is like the copy-and-paste buffer you have used in lesser gui tools. When you delete, the deleted text is saved for pasting and you can get it back by pressing the p (mnemonic: put or paste) key. The delete key can take another register if you want to specify one. A register is specified with a double-qoute character, followed by the name of a register (which is a lower-case or upper-case letter, where case is significant). That means you may copy multiple bits of text by yanking or deleting them into different register and pasting them into a new place (or new file) by using a register-specific paste.

Pasting also takes a register specification, which is always a double-quote followed by a letter, followed by the 'p' for paste. You could even paste a register many times if you specify a repeat count (see the lesson on the basic command pattern).

You actually have more registers than I told you, and can do more with them than I said, but this is enough for a quick lesson on deleting.

No comments:

Post a Comment