Vim registers
2019-12-06
TL;DR
Go to the end to see the summary
Introducing Vim registers
Vim documentation says:
There are ten types of registers:
1. The unnamed register ""
2. 10 numbered registers "0 to "9
3. The small delete register "-
4. 26 named registers "a to "z or "A to "Z
5. Three read-only registers ":, "., "%
6. Alternate buffer register "#
7. The expression register "=
8. The selection registers "* and "+
9. The black hole register "_
10. Last search pattern register "/
It may seem a lot, (and indeed the documentation for registers in Vim is a bit
complicated for my taste) and one may not use them all (at least not all the
time), but I'll try to describe them here in simple words. Of course I'll
leave a lot of functionality and corner cases out of this description. For
that, :help registers
.
Remember that to interact with them you usually use "
plus the name of the
register (ex: "ad
deletes to register "a
). On Vim scripts, you can refer
to them (for read and write if allowed) with @
(ex: :let @a = "foo"
)
You can see the contents of all the registers with :registers
The unnamed register
This is the one you're already using all the time, for deleting and yanking. Everything goes here, no matter what it is, all the time (more or less, check docs for details). That's why you get frustrated when you copy something and then delete a character and override your copied text.
This is the register that is invoked if you do not specify a register.
So things like d
, c
, x
, dd
, y
go into here, and things like p
get
content from here.
The numbered registers
Those are of 2 types really:
"0
Any yank command goes here (and to""
too) unless you specify another registry."1
to"9
Any delete or change command goes here unless you specify another register or the change is "small" (less than one line). This works like a queue, when you delete again, goes to"1
and what was in"1
goes to"2
, what was on"2
to"3
... until what was on"9
gets discarded. So you have some sort of "delete history"
The small delete register
Here go all the delete operations less than one line long unless other register specified.
Named registers
They are not automatically populated. You can use them explicitly to have 26 "clipboards" if you like. Keep in mind that they are shared with the macros. When addressed upper-case, they append the content to the register instead of replacing it.
Useful for reorganizing blocs too, you just append blocs of text to one register in the order you want and then paste it in the place you want it to be.
Read-only registers
".
Last inserted text. Just like the "dot" command."%
Name of the current file."
Most recent command line.
Alternate file register
Contains the name of the alternate file (CTRL-^
), the one marked #
on :ls
Expression register
This is not a register like the others. It allows you to execute an expression
that is converted to a string and put in place with p
or automatically if you
are in insert mode and invoke it with CTRL-R
.
Selection registers
Those are your system clipboards. Vim has to be compiled with support for this. One is the primary and the other is the clipboard (X11 stuff, you know). I never remember which is which, so I have some aliases like this:
" copy and paste from system easily
if has("mac")
nnoremap cp "*p
xnoremap cy "*y
else
nnoremap cp "+p
xnoremap cy "+y
endif
Black hole register
This is like sending stuff to /dev/null
The last search pattern register
Contains the most recent search pattern. Not sure of its utility, but it's there.
Summary
- If you do not specify a register,
""
(the unnamed register) is used. - Your last copy is always available at
"0
. - A "delete history" is available on
"1
to"9
. - Less than one line deletions are available on
"-
- There are 26 "clipboards" in Vim (
"a
to"z
) that can be appended addressing them upper-case. - You can evaluate expressions (like basic arithmetic and more) with the
expression register
"=
- Interacting to and from the system clipboard is possible with the selection registers.
- If you have something sensible that want to delete, use the black hole
register
"_
- You can check the contents of all the registers with
:registers
Hope it helps, and I hope I do not forget now.
Have any comments ? Send an email to the comments address.