Copying and Pasting, called “Yanking” and “Putting” in Vim parlance, is one of the most annoying parts about Vim for the beginner Vim user. While immensely useful once one gets accustomed to the way Vim does it, the dissonance between users’ expectations of copy and pasting in a modern environment, and what Vim does, is the cause of much consternation to the beginning Vimmer. This article endeavors to teach you the differences between “Copy and Paste” and “Yank and Put”, and along the way you’ll learn about a very powerful concept underneath the hood of Vim: registers.
We assumes that you have knowledge of being able to enter Vim, move around, and inserting characters into a document via insert mode, but requires no other prior knowledge. If you don’t, the previous tutorial in this series will help you get up to speed. Now, let’s get started.
Hit i
to enter insert mode, and type the following sentences, then hit the <Esc>
key to exit insert mode.
I have discovered that Vim is the best! Even though it's slightly annoying. But it's still worth learning.
Great! Now you have a set of text you’re able to practice on. Let’s review the main yank and put commands.
yy will yank, or copy, the current line. p will put, or paste, either the last yanked or the last deleted line on the below line. P will put, or paste, either the last yanked or the last deleted line on the above line.
Try it for yourself. If you’re in insert mode, first hit <Esc>
to move back to normal mode. Move the cursor to the bottom of the document by hitting G
, and then press yy
to yank (copy) the line, and p
to put (paste) the line. Your text should now look like this.
I have discovered that Vim is the best! Even though it's slightly annoying. But it's still worth learning. But it's still worth learning.
Yanking and Putting works like copy and pasting except for one key difference. As stated above, p
will insert either the last yanked OR the last deleted line. This is because Vim saves the contents of whatever you delete and whatever you yank, but chooses the last yanked or deleted line to insert. To demonstrate this, hit G
to go to the bottom of the document and press yy
to yank the last line. Then, hit gg
to go to the top of the document and press dd
to delete the first line. Then hit P
.
What was inserted? The last deleted line, not the last thing you yanked. This is useful because often, when you are coding, you’ll want to move pieces of code from one line to another, and often the easiest way to do it is to delete a line with dd
, jump up to where you want to put it, and then press p
. But what if you want to yank something, delete something else, and then paste in the yanked line, rather than the deleted line?
There’s a simple way to get around that, and it’s to type "0p
, but to understand why we need a bit of context. Hit <Esc>
if you aren’t already in Normal Mode, then type :reg
and hit enter.
A list will come up named “Registers”, which is what :reg
stands for. In Vim terms, a register is just a single block of memory to store things, usually things you delete and yank, and Vim has dozens of them to store whatever you would like. In the list, at the top, you’ll see ""
with the last thing you deleted, and then "0
with the last thing you yanked. This is because ""
signifies the last used register, no matter what action you just performed. If the last action you performed was to yank, then ""
will hold the copy of your last yank. If it was to delete, then ""
would hold the last thing you deleted. But "0
will ALWAYS hold the last thing you yanked, no matter how many things you delete.
This is important because if you want to yank something, delete something else, and then put (paste) what you yanked and NOT what you deleted, you can use the last yanked register with the following commands.
"0p will put(paste) the last thing you yanked. p will put the last thing you either yanked or deleted.
There are many, many more registers than just these two. In fact, there is one for all the letters in the alphabet. When you hit "
, you tell vim “Okay, I want to use the following register to do something”, and then when you type 0
, you let it know that 0
is the register you would like to use. When you type the p
command, Vim will notice “Hey, the user just said he wants to use register 0, and now wants to put something into the document, so let’s put the contents of the register they asked for”, and puts (pastes) the text you’ve yanked. This is a compose-able command, or a command made of multiple different sub commands. This is a very simple compassable command, but if you would like to read more, the next article in our series goes over composable commands more in depth.
For now, hopefully you’ve grasped the concepts in this lesson and are able to efficiently copy and paste in Vim.
Leave a Reply