Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
You’re probably feeling very confident to cut (x), copy (yank y) and paste ( 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 is 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’re gonna focus 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 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 you're gonna use.
Let’s say you want to copy line 1 and store it on register d , you'll go this line and type: "dyy
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
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 .
you can paste it to other files / buffers tooAppending text to a fillfilled register:
It’s easy to append more text to a named register that already has text on it. To do that you’re gonna 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
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)
Footnotes:
- If you have any doubts or tips I’d appreciate to know and discuss it on the comments section.
- Do you have any other vim tips and want to publish it here? Send me and email at ‘vimdrops AT pm DOT me’ with your medium username and I’ll invite you as a writer!
- As English is not my first language, I apologize for errors. Corrections are welcome at ‘vimdrops AT pm DOT me’.
Originally published at Vim Drops April 16, 2018.
Learn the Basics of Vim Registers, Your Trusted Native Clipboard! was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.