How Well Do You Know Vim Registers?
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".
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.
:register "
— using a new buffer will come in handy later 😄- Open a new buffer with
:new
. This buffer will appear on top. Put
the value of theregister
withput
and the name of the register. In this case,put "
.
Voila! We see the contents of our register "
.
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
.
So let's see what we can do here.
- Go to line #2 with
: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 pressingy
. We'll do this by pressingv
and then moving the cursor to the right withl
(LDUR). Be sure to stop on the letterC
as going beyond it will also take the\n
character.
- Move up to the first line with
k
. - Instead of
ciw
which would change this word, let's just simply delete it withdiw
.
- 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.
"
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.
- Select the entire file by using
gg"+yG
.gg
goes to the first line."+
specifies the+
registeryG
- yanks the entire file
- Go to Slack or email and paste from your system clipboard as normal.
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.
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.
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.
Now we'll use a simple replace to do our work for us. The final command is :%s/param/arg/g
.
- Enter command mode with
:
%s
- the substitute command applied to all lines
%
and only use :s
. - Type the
/
but instead of typing the wordparam
, you can paste it in from the/
register withCtrl+r /
. /g
- this applies the search and replace across the entire file.g
in this case meansglobally
.
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.
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!
Tip Jar!