Vim Registers

I’ve been using Vim registers a lot more recently. This post is my cheat-sheet for how registers work in Vim and a few useful actions:

Copy/Paste

Anytime you copy/paste text in Vim you are interacting with a register. I’ll often rely on being able to paste a line after I’ve just deleted it with dd. For this to work I cannot delete any other characters after the initial delete. If I did this would overwrite the line I want to keep.

What’s actually happening here:

  • dd deletes the line and moves it in to Vim’s default register. This register is known as the unnamed register.
  • p pastes whatever data is in the default register.

The unnamed register is the resting place for characters which are deleted or yanked, unless the user says otherwise.

That’s not the whole story though. Vim has a bunch of default registers which are genuinely useful in day-to-day Vim tasks if you know about them. Picking up the example at the start, if you do delete text and then delete more text your previous contents from the default register are not lost (as I used to think was the case). Vim has numbered registers as well, 0 to 9. Contents just deleted start at 1. When the next delete is made everything is shuffled up one place (with the contents of 9 actually being lost), well mostly…

View register data

This is perhaps best understood by looking at the contents of these registers. You can do this from Vim by running :register.

  • All registers start with "
  • "" is the unnamed register. Text you delete/yank ends up here by default as mentioned.
  • "0 will always contain the text you last yanked.
  • "1 will always contain the text you last deleted.

This means if you accidentally delete text after you’ve yanked or deleted a line you want to paste elsewhere all is not lost. Register 0 will still contain the last thing you yanked, regardless of subsequent deletes.

Viewing just a single register: Another way to view the contents of the register, e.g. register 5, is to run `:echo @5`.

Accessing other registers

When you perform a delete/yank/paste command, Vim is behaving the same as if "" is prefixed to the start of your command sequence. To access another register you can run "2p for example, to paste out the content of register 2.

Macros

I wrote a brief article sometime back on macros in Vim. Macros are usually stored in registers in Vim. This might not sound particularly useful but you can edit the macro commands by editing the register contents. If we have a complex macro to reformat a line, and we then want to adjust this slightly, it is likely an easier operation to edit the commands looking at the character sequence in the macro than it is to record the entire sequence from scratch.

Get the current filename in Vim

The % register in Vim holds the name of the current file. I can’t remember why I needed this now but it was useful enough to me last year that I scrawled this fact on a post-it note on my desk. An alternative that kind of works is to :w and just see where the file is written out to, though arguably the potential for unwanted consequences make this far from ideal!

Access register contents in command

A final useful register tip. If you have a Vim command where you want to use the contents of a register, e.g. search for the text I just deleted, you can access the register contents by typing ctrl-r [register-name], where the full command to search would be /[ctrl-r register-name]. This will show you contents of the register as part of the command, rather than auto-executing.

Dev Vim
Back to posts