Step 6 of 7

6. Copying And Pasting

The zero register

Vim also has registers for the numbers, 0 through 9. These are managed by Vim, and are filled with text from the yank and delete commands.

In this section, we’re going to focus on the "0 register. This register is particularly special because it always contains the text from the most recent yank command (unless that yank command used a specific register).

This is useful because, if you recall, the default register is populated with the contents of deletions and changes too. As a consequence, it’s very easy to accidentally overwrite the text in the default register by accident.

In practice, this means that you can use "0p to put the last text you yanked, no matter how many deletions and changes happened afterward. "0 is only ever written to when you yank, so you don’t have to worry about deletes and changes overwriting its contents.

Work through the checklist below — the line you put at the end should be the one you yanked at the start, untouched by the deletions in between.

  1. Yank the first line Put the cursor on the YANK ME line (4G gets you there) and press yy.
  2. Delete the three lines below it Each deletion overwrites the unnamed register — move to the first Delete me! line and press dd three times.
  3. Put from the zero register The yanked line will come back untouched by the deletions — move to the bottom of the file and type "0p.
Yank the first line Put the cursor on the YANK ME line (4G gets you there) and press yy.

Next: Delete the three lines below it

Loading editor…