Skip to main content

How Well Do You Know Vim Registers?

Typecraft

Registers...for those new to vim, registers are one of those concepts you may have a loose understanding of.

When you think of registers, you might think "yeah, those are the things you use for copying and pasting." This is true but only scratches the surface of everything that registers can do for us.

For those more well-versed, stop and consider how many different ways we use registers. Count them up and see if you've covered them all. Most developers learn something new!

What are registers though?

Naming is hard in software development, and registers aren't any different. Think of a register as a book on the shelf or a page in one. What do you find in books or pages?

Text.

That's all.

When people talk about storing things in a register, you should think of taking a book off the shelf and writing some text in it. Or maybe it's taking a book off the shelf and doing something with the text in it.

So what are registers? A place where text is stored.

Now, let's see how we can use these books.

1. Copying and Pasting

Whenever you perform a yank, delete, or paste operation, you make use of the "unnamed" register. This unnamed register goes by the name ". That's right. A literal double-quote. Let's see it in action.

Open vim and write any word. I'll use the word "unnamed".

Unnamed -- how creative. I told you naming was hard.

Now let's yank this entire word with yiw. Make sure your cursor is somewhere on the word.

How can we verify that the unnamed register has this value? Anytime you want to know what's in a register, you can open a new buffer and put the value.

💡
You can also see the contents by simply use :register " — using a new buffer will come in handy later 😄
  1. Open a new buffer with :new. This buffer will appear on top.
  2. Put the value of the register with put and the name of the register. In this case, put ".

Voila! We see the contents of our register ".

The top is our new buffer. Below is our original content.

2. 0 (zero) Register

Now remember that the unnamed register stores the last text that was yanked or deleted. This can pose a problem if we want to yank a word, delete another, and then paste the originally yanked text.

Time for an example.

Let's say you have some text you want to replace. In our example, we've written some contracts that use Typecraft in some places, Typecraft, LLC in others, and finally TypecraftDev.

We need to update all references to say Typecraft, LLC.

Consistency is important.

So let's see what we can do here.

  1. Go to line #2 with :2.
  2. We'll assume that in a real document, there would be other words on this line. Instead of using yy, which would yank the entire line, let's yank it by first visually selecting the text and then pressing y. We'll do this by pressing v and then moving the cursor to the right with l (LDUR). Be sure to stop on the letter C as going beyond it will also take the \n character.
Visually select
  1. Move up to the first line with k.
  2. Instead of ciw which would change this word, let's just simply delete it with diw.
By deleting this word, our unnamed register will now have the word "Typecraft". Remember – deleting a word overwrites the unnamed register.
  1. Paste the contents of the 0 register. To do this, we have to specifically name the register we're pasting from We do this using this syntax. "0p — let's break this down.

" - tells vim we're going to perform an operation using a specific register. In our case, we're going to use the 0 register in a paste (p) operation.

We can repeat these steps for to replace TypecraftDev. Why is this important? Because simply using p will paste the text we just deleted! 0 only gets overwritten with text that is yanked.

3. Registers 1-9

Whenever you delete something, it not only goes into the unnamed register. It also goes into a stack. The stacks are registers 1 through 9.

When you delete something, it goes into register 1. Anything that was previously in that register gets pushed down to register 2. This allows you to retrieve the last 9 deletions!

4. Named registers a-z

These are books with names! Or, in our case, letters. These are only written to when you specify them. Let's see how we might use this.

💡
Remember, you have to use " and then the name of the register.

a. "ayw - yank a word into the a register.

b. "ap - paste the contents of the a register

5. The + register - system clipboard

Let's say you have a method you want to send over Slack to a co-worker. Sure, you could open up VS Code quickly to copy and paste the code, but let's just yank the code visually.

The difficult code
  1. Select the entire file by using gg"+yG.
    1. gg goes to the first line.
    2. "+ specifies the + register
    3. yG - yanks the entire file
  2. Go to Slack or email and paste from your system clipboard as normal.
Don't want the entire file? Visually select the code you do want and then specify the register before you yank (y).

6. The . register (last inserted text)

This one is pretty straight forward. If you find yourself repeating yourself, you can insert the text to repeat in one section and then, while in normal mode, press . to insert the same text over again. This is a read-only register so you can't put your own text into this manually. It's all automatic.

💡
In insert mode and want to paste the prior inserted text? Use Ctrl+r and then the . register.

7. / register (search patterns)

Want to use your last regex to do a replacement? You don't want to write it over again, do you? Just use the / register.

Ridiculous example

Let's assume that you've come to the realization that param1 and param2 are terrible names for arguments. You've decided that these should be arg1 and arg2. Wouldn't be my choice, but let's fix it with a regex!

First, let's search to see how many times we've used param as a prefix. We can do this with /param. Yes, this is a simple regex.

So many times!

Now we'll use a simple replace to do our work for us. The final command is :%s/param/arg/g.

  1. Enter command mode with :
  2. %s - the substitute command applied to all lines
Want to replace only on the current line? Don't use the % and only use :s.
  1. Type the / but instead of typing the word param, you can paste it in from the / register with Ctrl+r /.
  2. /g - this applies the search and replace across the entire file. g in this case means globally.

8. % (current file name)

Ever wanted to have the name of the current file? No problem! Just use the % register. Keep in mind that what it outputs can vary based on how you opened the file.

For example, if I'm in the folder ~/src and I open the file under the subdirectory typecraft/something_difficult.rb, I'll see the path relative to where I opened Neovim.

Path relative to the directory I opened Neovim in.

9. _ - the black hole register

As you can see, registers get overwritten when you perform operations. Yanking a word? Deleting a word? These can all impact your registers even if you don't want them to. Sure you can use registers like the 0 register or the delete stack 1-9. Sometimes though, you just want to perform an operation and not update any of the registers.

No problem! Just prefix your operation with "_ and you'll make sure it goes into a black hole.

Be Honest

So, be honest, did you know all of these register operations? What's your most unusual use case?

Hop in our Discord and join the conversation!

Join our discord Tip Jar!