Vim Registers, The Powerful Native Clipboard!

Learn the basics and boost your productivity. (Plus: a command to clear all registers at once!)

image

The command :registers lists the registers that are being used.

You’re probably feeling very confident to cut (x), copy (yank y) and paste (put p) on vim, and now it’s time to take this operations to another level using registers!

In fact, registers are already being used when you perform the operations mentioned before. Registers are everywhere on vim! When you yank, delete, paste, cut, when you edit text, when you type a command in normal mode, it’s all being registered.

Vim has 9 types of registers with different characteristics (you can check typing ‘:help registers’), but we’re not going too deep, we’ll be focusing on the basic registers, the named registers.


What is Vim Registers?

You can think of registers as a functionality on vim that exists to store information. Vim uses it registering commands you enter, files you open, text you input, etc. What vim does is to save all this data on a .viminfo file while you’re working (relax, you’ll never need to touch this file).

Vim allows you to use this functionality, this powerful clipboard, to store different texts on different addresses like:

"a — Hello,

"b — World

"c — Again!

…and even allows you to append more text on a register that is already fullfilled:

"a — Hello, + to all the people = "a — Hello, to all the people

…and then it’s there, saved on your clipboard, to paste it the where and when you want to.


How to use it:

From all the different types of registers, we’re gonna focus on using the named registers, from a to z . You can use this 26 letters as an address to save text on your clipboard, and later call the specific address you want to paste.

What you’re used to do:

  • To copy (yank) a line: yy
  • To delete a line: dd
  • To paste (put) a line copied: p

Things are almost the same with registers, you’ll just need to set the register you’re gonna use before typing the command.

How to save things:

You just need to type " + the letter you picked as address for that specific text you’ll saving, and then type the command to perform the action.

Let’s say you want to copy line 1 and store it on register d, just go to this line and type: "dyy

image

The line 1 was saved at register d.

Now, you’d like to save line 3 on register a, and delete line 5 but saving it on register g :

  • Go to line 3 and type "byy
  • Go to line 5 and type "gdd

image

Line 3 was stored on register b, and line 5 was deleted but saved on register g.

(You can always use the command :registers to preview your clipboard)

How to paste things:

The same way you record a register is the way you’ll paste a register. If you want to paste line 1 (stored on d), and then paste the deleted line 5 (stored on g), type "dp , and "gp .

image

You can paste it to other files / buffers too.

Appending text to a fulfilled register:

It’s easy to append more text to a named register that already has text on it. To do that you must use the uppercase letter when performing the operation.

Let’s say you want to delete line 6 but appending the text on register b where you already has text, and after, you want to paste this new b register to the other file.

  • Go to line 6 and type "Bdd
  • Go to the other file and type "bp

image

Done! You’ve learned the basic usage of registers. It’s enough to give you more flexibility, boost your productivity, and you have opened new doors. Now you can hit ' :help registers ’ and learn more!

Special Trick: Clear all registers!

Vim carry on registers to other sessions, it saves all registers on .viminfo file, as mentioned before.

If you don’t want to get a messy clipboard, or you just want to start fresh at some sessions, there’s a script you can add on your .vimrc file that creates this command :WipeReg , and it will always be on hands for whenever you feel like wiping all registers.

  • Open your .vimrc file and paste this command in one single line:
command! WipeReg for i in range(34,122) | silent! call setreg(nr2char(i), []) | endfor
  • Type :WipeReg whenever you feel like cleaning up the table!

(This trick was posted on Stack Overflow, thanks laktak)


(👆 This is an affiliate link. If you got any value from this post and are also interested in buying the MVQ book/screencasts, please consider visiting the affiliate link and you’ll be gifting me a coffee ☕ or a beer 🍺.)


Footnotes:

  • Follow me on Twitter to get more posts like this and other quick tips in your feed.
  • alldrops.info is the TRUE and FOREVER FREE home for the previously used medium.com/vim-drops publication. At alldrops.info you’ll always find all posts without Medium.com restrictions.
  • If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
  • Do you have any other Vim tips? Would you like to publish that in this blog? Please send an email to vim drops.
  • As English is not my native language, I apologize for the errors. Corrections are welcome.
  • Contact: vim [@] alldrops [.] info.

Read more on vim drops: