Lesson 23 of 26

Understanding Vim Registers

Duration: 8:37 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy lists registers with the reg command and pastes from one by typing a double quote, the register name and p, starting with the percent register, which holds the current file name. He shows the double quote register with the last copy or delete, the numbered registers for earlier deletes, the colon register for the last command, named registers such as a that keep a yank, and appending to a register with an uppercase letter. He also covers the black hole, small delete, last search and equals registers, and clearing all registers by removing the viminfo file.

Transcript

1,293 words

[00:08] (external link, opens in new tab) Let's talk about registers in Vim. To understand what registers are, the first thing we're going to do, we're going to type the command reg in the command window by typing colon reg and enter. And we see this list of just one item called registers. And this one item points to the current file name. And it starts with double quotes and then percent sign. Double quotes is how we access registers that contain meaningful information inside Vim. And the percent sign in this case is one special register that always contains the name of the current file.

[00:40] (external link, opens in new tab) So let's say I want to paste the name of the current file inside my current file. I'm going to press enter to leave this. And if I wanted to paste it, all I have to do is to use again the double quotes to access a register, percentage to tell Vim which register to access and then P to paste it or shift P if I want to paste it the line above etc. So let me try this double quotes percent P and I get the name of the file.

[01:12] (external link, opens in new tab) Okay, now I'm going to actually type DD to delete the current line. And now I'm going to type the reg command again. And this time we see the list has changed. First of all, we see that the first item is a double quote double quote, which is again a special register. The special double quote register always contains the last thing that was either copied or deleted

[01:42] (external link, opens in new tab) So that we can paste it again later on. And then we have a double quote one which also contains the text of the thing I just deleted. And this is what's called a numbered register. And then and as I copy more and more things you're going to see double quote one, double quote two, etc. which basically allows me to go back nine places in memory to the things that I deleted before. So I can kind of see and retrieve up to nine things I deleted without even doing an undo.

[02:16] (external link, opens in new tab) The third thing the third register you see is double quote colon which basically is another special register that tells me always contains the last command that I executed basically is a string and again the last one is the name of the file. So, we have some of these special registers and I'll talk about some better overview registers later on as well. But let's see what else we can do.

[02:48] (external link, opens in new tab) I can create my own registers. For example, I can go down in the file and let's say select these two lines and I can save them into a specific register by again saying double quotes the name of the register which could be for example the letter A and then pressing Y for yanking for copying. So let's try that and see what happens. Double quotes a yank.

[03:18] (external link, opens in new tab) And what happened now is that I have register reg which is the letter A that contains the text that I just copied. So now no matter what, no matter if I've deleted or copied other things whenever I want, I can always paste from this special register. So for example, I can go down in the file by pressing Ctrl + D writing a new line here. Escape. I just want to paste that again.

[03:48] (external link, opens in new tab) Or you know what? Let me copy some more things. Shift V, go up, yank. Just yank them into default. If I try type reg now, you'll see that my double quote double quote actually contains the last copy, but it's different from the double quote a. That's always saved. So, I can always go back and paste just the A1 by saying double quote a paste, please. and I get that thing. So you can have basically all the letters and a bunch of characters, a bunch of symbolics as well as registers

[04:26] (external link, opens in new tab) Which allows you great flexibility. Another thing you can do is instead of replacing you can append to a specific register. So for example, if I have V selected just these four characters and I say double quote B yank. Now what's going to happen if I press reg is you can see that under the B right here I have the word dark.

[04:57] (external link, opens in new tab) Let's say I wanted to append to that. I can say instead of just using the letter B, I'm going to use an uppercase B which appends to it. So I'm going to say double quotes B yank. And since I only have D selected right now, if I press reg, oh, I didn't have anything selected. So let's select something else. Let's select this word ve. So to select until the end of the word and I'm going to say double quotes uppercase yank

[05:32] (external link, opens in new tab) Which if we look at reg now for the register B we now have dark and mids appended to each other. So we can append to existing registers easily. One way to get rid of existing registers is a way that I've not shown yet, which is basically recording a macro that doesn't do anything into that specific thing. And another way if you wanted to clean out all your registers, what you do is you

[06:08] (external link, opens in new tab) Remove the viminfo file which you have in your home folder which contains among other things the location of of all the registers and their contents. So just to recap here are some of the register we talked about and a couple we haven't. First we have named registers and you just use the double quotes to type double quotes the name of register in an action. For example, you want to copy the whole line.

[06:38] (external link, opens in new tab) You just say double quotes a and then y will copy the whole line into the a register. And you have the default register which always takes the last delete or the yank or the copy. Basically we have the numbered registers which is basically the most recent deleted text. We have black hole register. Basically, if you want to discard of something, you don't want it to get saved to a register, you save it to the black hole register, which is kind of like dev null in Linux. And the idea is that nothing that goes in never comes back basically.

[07:10] (external link, opens in new tab) Then we have a small delete register. So, if you've deleted something that's shorter than a line by pressing X, for example, or just selecting a few characters inside a line and deleting them, they're going to get saved in that special double quote minus. The last inserted text register will always be at the double quote dot, but if you just want to paste the lasted the last text you pasted before, just use the actual dot command instead of the register. So, you don't need to use this one.

[07:42] (external link, opens in new tab) The current file name will always be a double quotes percentage. The most recent command will always be a double quotes colon. The most recent search that you've done will be double quotes slash forward slash. And there's a special kind of a weird register that saves the results of other registers. So if you want to say double quotes equals and basically put in the result of something for example double quotes equals 1 + 1 you will actually have the number two inside the register if you wanted to paste it and this might be good for you if you're doing any kind of programming or like special plugins you that might be useful to you otherwise just use a simple calculator even in command line.

[08:28] (external link, opens in new tab) So that's it for registers.