Cut, Copy and Paste – Part 2 – Registers
Cut, Copy and Paste – Part 2 – Registers

This course covers deleting, yanking, and putting in Vim, otherwise known as cutting, copying, and pasting, respectively. We cover the essential commands for carrying out those actions to allow you to move text around in Vim seamlessly. We also look at registers where deleted text is stored for future use. Finally, we will walk you through a demonstration of these actions for you to follow along with.

Intended Audience

This course is ideal for anyone who needs to edit files in a command-line environment.


If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.


If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.



Up until this point we've been using the unnamed or default register to perform our cut, copy and paste operations. There are actually many type of registers in VIM, but we're going to focus on the next two most commonly used registers. The first is numbered registers, and the other register type is called, named registers. Registers are proceeded with a double quote. The unnamed register is actually double quote, double quote. The numbered registers are zero through nine. So register zero is double quote zero, and register nine is double quote nine, and so on. The unnamed register contains the last bit of text from an operation like delete, or yank as you've already seen. But to be completely accurate, VIM fills the unnamed register with text copied or deleted with a D, C, S, X and Y commands. Although we haven't covered all of them yet. Quickly C is the change command, and S as the substitute command. In any case register zero contains the text from the most recent yank operation, and register one contains the most recent texts, from a delete or change operation. With each success of deletion or change, VIM shifts the previous contents of register one into register two, register two into register three, and so forth, losing the previous contents of register nine, it falls off the end if you will. Let's put this into action. Let's move down to the line that starts with, TODO. Now let's yank it with Y, Y. Y, Y to yank and now to look at the registers we can type colon reg and press enter. The first register you see is the unnamed register represented by double quote, double quote. Remember any texts yanked or deleted will be stored there. Also notice that register zero contains the yanked text as well because, register zero always contains the text from the most recent yank operation. Executing the put command duplicates this line, as expected. So we'll press enter here, and press P, and the line is duplicated. Now let's move down a line and delete that with D, D. We'll move down one with Jay and type D, D. When you look at the registers now with colon reg enter, you'll see that the unnamed register contains the deleted text as well as register one. That's because register one stores the most recent deleted text. Also notice that register zero still contains the text we yanked. Let's press enter to return to normal mode. And let's see what happens if we execute the put command, capital P, Shift + P, that's right. The line you deleted is pasted, because it was what was stored in the unnamed register. But let's say what you really wanted to do was paste the yanked text so, you can undo your mistake with U. We'll do that now. U, undoes that mistake. Now to paste the most recently yanked text, precede the put command with the register. So type double quote, zero, followed by Shift + P. Now the desired text is pasted. Can you see how storing the most recent yanked texts in one register while storing the most recent deleted text in another is useful. If you find yourself needing to paste the same thing over and over again, while deleting and cleaning up things in between those times, you'll find yourself using the zero register. Another way you can approach this is to use what's called as the black hole register. The black hole register is represented by double quote underscore. When writing to the black hole register, nothing happens. This is used to delete texts without affecting the normal registers. Let's yank this current line with Y, Y. When you look at the registers, we'll do colon reg, and hit enter. As expected the text was stored in the unnamed register and the zero register. Press enter to go back to normal mode. Now let's move down a line and delete it. But this time use the black hole register. To do that we'll press J to go down a line, type, double quote underscore which represents the black hole register and then type D, D, which is our command. Now let's look at the registers. We'll use colon reg and press enter. Nothing changed in the registers. Normally a deletion operation would cause the unnamed and one registers to be changed. But when you use the black hole register nothing happens, press enter to go back to normal mode. And now you can paste the TODO text using the unnamed registers. So just type P and TODO is pasted or put, on the line below the cursor. Again, with VIM, there is usually more than one way to achieve the exact same end result. Just use what makes the most sense to you. If you like the idea of the black hole register, well then use that. If you want to use the default behavior of yank texts being placed into register zero, and deleted texts being placed in register one, go ahead and use that. Now let's move to another line and delete it with DD. Now when we look at the registers, we see how it's placed in register one, while what was previously in register one, is now in registered two and so on. So if you've recently deleted something and you want to use it again, simply look for it in the numbered registers. For example, let's say we want to paste what's in register two, the line that says this line needs to be fixed. So we'll press enter to go into normal mode, and then type double quote, two and P, and that line gets pasted. That's numbered registers in a nutshell. But before we move on, I want to point out one more thing. Let's yank the next three lines into the unnamed and zero registers with three YY. But first position my cursor down here and do three YY. Now let's look at registers with colon reg, and pressing enter. When you looked at what we just yanked, you'll notice a caret J symbol, that simply represents a new line character. When you paste that text, that caret J, will be replaced with an enter effectively. I'm going to reposition my cursor down here a couple of lines, and then put this text toward the top of our screen with Z, and hit enter. Instead of relying on the numbered register behaviors, you can use named registers. There are 26 named registers from A to Z just like you can specify a register when pasting, you can specify one when yanking or deleting. Let's yank the next line of text into the A register. And to do that, we'll you use double quote, A Y Y that yanks the entire line into the, A register. Now let's look at the registers with colon reg and press enter. You can see that that text is indeed stored in the A named register, which is represented by double quote A. We'll hit enter, and go back to normal mode. We'll move down to the next line with J, and this time let's store this text into the B register. So we'll do double quote BYY. Now let's look at the registers again, reg, enter, and you'll see the text that we just yanked in the B register. Hit enter to go back to normal mode. Now you can pace from the A register with double quote AP, as well as the B register with colon BP. If you want to append more texts to the register, use the capital letter of the register name. So let's move down a line here, and we'll go to this line. And then type, double quote, capital A, which has shift + A, YY. Now let's look at the registers, reg enter. You can see that the line was upended to the existing text. If you were to use lowercase aid, then the texts would have been replaced. But since we used uppercase A, it upended to the A register. Let's do the same thing with the B register. We'll move down the line, double quote, capital BYY. Look at the registers. And again, you can see that it was upended, to the B register. Remember that you can use registers with a delete operation too. So to delete the word zebra, into the Z register, we would just position our cursor on that word, and type double quote ZDW. By the way, you can look at a specific register, by supplying it as an argument to the reg command. So if you wanna look at just register Z, we'll type colon reg Z, and press enter. If you wanna look at multiple registers, just list them. Let's try this again, we'll do, colon reg. Let's look at register one and Z, and press enter. You already know how to repeat a command multiple times by starting that command with a number. Working with registers is no different. Again, you're thinking in them, you can use count register operator or, register count operator. For example, let's move down to the line of hyphens. I'll do that and reposition the text higher in our screen, with Z and hit enter. To yank that into the H register. We can run, double quote, HYY. If we wanna two lines of dashes, or those hyphens there, we could run two, colon, double quote, HP. That simply executes the command twice. You can also use double quote H2P, which does the exact same thing. You've seen it done this type of thing before. So I'm not going to belabor the point here. You've learned a lot in this lesson. So let's take a couple of minutes to quickly recap what you did learn. Remember that in VIM's speak, cut, copy, and paste, are actually delete yank, and put. Registers are used to store and retrieve text. VIM's unnamed register sometimes called the default register, makes it easy to quickly cut, copy and paste. Or should I say delete, yank and put. The unnamed register contains the contents of the most recent deleted or yanked text. When no register is specified for a command like put, the unnamed register is used. The unnamed register is represented by double quote, double quote. There are 10 numbered registers ranging from zero to nine. Register zero contains the text when the most recent yank operation and register one contains the most recent texts from a delete or change operation. With each success of deletion or change, VIM shifts the previous contents of register one, into register two, two into three, and so forth, losing the previous contents of register nine. In addition to numbered registers, there are 26 named registers, ranging from A to Z. With named registers, you have full control, over what goes in each of the named registers. You can even append to a register by using the uppercase version of the register. So to upend to register R for example, use double quote capital R when deleting or yanking. To display the registers use the colon reg command. You can specify a register or a list of registers to display by passing them as arguments to the reg command. Otherwise all the registers are displayed. In addition to all the information you learned about registers, you also learned how to undo a command with U, and to redo a command with Ctrl + R. The undo and redo operations undo or redo an entire command, no matter how much or how little text was affected by the command.

About the Author
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.