How to Copy and Paste in Visual Mode
In this guide you'll learn how to copy and paste items while working in Vim's Visual mode.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide I'm gonna take a little bit deeper dive into how we can use copy and paste and those kind of concepts using the visual mode. As a review, and I'm just gonna type some of these commands, so you can see 'em. These are all like I've mentioned before in the cheat sheet so that you can access them, but just so you have a little bit of an idea the concept of copying, which is called yanking, you can activate while in commander visual mode with a Y and this is gonna be to yank a single element, YY is going to yank the entire line.

Then if you want to cut, that is the same as a delete, so if you want to cut one item that's been selected, then this is going to cut/delete selected item or element, and then DD will cut/delete an entire line.

large

So hopefully that kind of helps clarify it, I know this part can be very confusing if you've never done it before. But, let's see what we want to do when we want to copy multiple lines. So, the most practical way of doing that is by leveraging visual mode. So, say that we want to take these little shortcut items, that I wrote right here, the way we can do this, is by pressing SHIFT + V, so capital V, that's gonna select that entire line, and then I can use my navigation keys.

So I can use J, J, and J, and what this is going to do is it's gonna let me select all of those items, and then, I can do whatever I want.

large

So, here, I don't need to use YY, that'll actually give some weird behavior, remember at the very top, I said that Y yanks a single element, well when you select multiple lines, those all get grouped into a single element, so I can just press the Y key, and now if I come down, notice that automatically switched me out of visual mode, and on the bottom left hand side, it says, four lines yanked.

Now, if I want to paste this in, I come to wherever I wanna paste it, and then I can just press the P character, and there you go, it's pasted each one of those in. Now what happens if I wanna delete all those items and cut them and paste them someplace else? I'm gonna follow the same process, I'm gonna do SHIFT + V, grab those items, press the D character once, remember we do not need to press it twice, and if you do, you're going to have some weird behavior, and I'll even show you, watch what happens if I press D twice. I'm gonna press D twice and then oh, I went to move the cursor down and it deleted the next few lines, so that's not something that you wanna do.

So instead make sure that you know the difference. Y yanks a single element, D cuts and deletes a single selected element, the double version, so YY and DD, these ones are only for if you want to remove an entire line. So here, I just want to grab these four, I'm gonna just press the D character, I'll come down to the very bottom of the file, and now I'll press P, and each one of those items has been pasted down here.

Notice how, if I come up to the top, these items have been moved outside of the method, they're no longer there, so that's one of the most important things, is understanding that D is not just for deletion, it is also for being able to cut.

So, that's why I wanted to spend some extra time and go through some examples, and also walk through the difference between the double version of each one of those so that you could kind of prevent some bugs, 'cause when I was first learning Vim, I taught it to myself, and there was a lot of trial and error when it came to working with it, and so I would have all kinds of weird behavior, and a lot of it would stem from doing things like pressing DD twice, and then trying to navigate, and it removing all the next set of items as well. Where as, here, when you keep the single form where it's just Y or just D, this let's you acts on single elements and be a little bit more targeted with the work you're doing.