Basic Copy, Paste, and Cut Commands in Vim
This guide examines how to perform copy, cut, and paste commands, along with how to copy/cut selected items along with entire lines of code by leveraging the delete and yank processes.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

One thing that many newcomers to Vim will ask me is how to work with copy and paste, and I've found that this is a topic that can be a little bit confusing, especially if you've never used Vim before, and it's because Vim uses a different type of clipboard than you're used to using. For example, if I want to bring in a paragraph of text, I'm gonna switch over to Lorem Ipsum, and I'm gonna bring back this whole paragraph right here.

Now, if I wanna insert this, then the way to do that would be, I'm gonna come down with a shift g to the very bottom and switch into insert mode, and then I can press command v, and that's going to use the system clipboard. So the system clipboard gives you the ability to copy and paste items using the popular command c and command v type of commands.

Now, Vim does not work that way. Vim does not have a command c and command v for copying and pasting. Instead, Vim uses the concept of yanking and deleting, and if that seems a little bit weird, that's perfectly fine, it takes a little while to get used to. So let's come up here, and I'm going to grab some content, so I'm going to select it by pressing the v key on the keyboard, and then I'm just gonna press w, and this is going to copy the first two words right here.

large

Now, the way that I can copy just these two words is by pressing the character y on the keyboard, and now if I come down, say that I wanna come down and I want to put it inside of something else, then right here, I can just press the character p, and that's going to put those items in.

large

I do need to bring it back, so I do it here, so press p and you can see that that gave us exactly what we wanted, add a space inside. So that's how you can copy and paste a couple of words. Now, you also are gonna have times where you wanna pull an entire line, and once again, if you talk to Vim people, they're really not gonna say copy and paste.

I'm saying that simply so that you can know the concept since it's familiar, but usually you're gonna hear the term yank, so if I come back up to this line, and say I wanna pull the entire line, there are two ways to do it. The first is to yank it, and to grab that whole line, I double press on y, so if I press y twice, I'm going to have yanked that, and then I can come down anywhere else in the file and press p just once, and as you can see right there, that pulled in the entire line, so that's how you can easily, quickly grab a line of text and copy it and then go move it someplace else.

large

Now, if you want to cut an item, so usually in Mac, or in Linux, or in PC, you do command x. Well, here, cut and delete are actually synonymous, and this is something that can really trip you up if you've never done this before, which is that if you wanna delete an item, and lemme actually run through a code example that could be kinda confusing.

Let's say that I wanna put this line of text right here inside of my method, so say that I wanna do that. I'm gonna copy that entire line, and then I want to delete all of this content, and the way you can delete all of it is by pressing d twice, the letter d twice, just like this. Now, if I come back here to hit paste, look at that, it actually took the line that I deleted.

large

So this can be something that can be confusing if you've never worked through this kind of concept before. Usually, say that you're working in Sublime, you would delete an item by pressing the backspace or the delete key, and then you would cut an item using command x. However, in Vim, the two items are synonymous, so that means that you really just have to kinda keep in your mind that if I wanna do something like that, the process I just showed you, I would copy it, I'd come down and paste it, and then, oh, I don't have my indentation settings set yet.

So I would come down, paste it, and then I would delete, so it's not really a hard thing to get used to, you just have to know the process, so that you don't have one item that you put on the clipboard overriding another one, and there are all kinds of plugins that you could add in that are great clipboard managers, where you can copy and paste as many things as you want, and then quickly grab the one that you actually wanted, just because this process right here can be a little bit confusing.

So, that is how you can copy a single word, how you can copy a entire line, and then also how you can delete an item, which is the same as cutting that, so those are some kinda common ways that you can do it, and in the next guide, I'm gonna walk through how we can do this same kind of process using the visual mode.