Deleting, Yanking, and Putting in Vim
Cut, Copy and Paste – Part I

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.



One of the things you'll find yourself wanting to do when editing files is to move texts from one location in a file to another location. Another thing you'll want to do is duplicate text. For example, if you want to change a line in the configuration file, you'll want to make a copy of that line and commented out, so you can have a little mini backup right there in the file itself. This way you can easily back out of your change if needed. Sometimes you'll just want to place the same bit of information into multiple places in a file. In short, there are several reasons why you'll want to know how to cut and paste as well as copy and paste. Even though you may not realize it, you already know how to cut text the d or delete operator you've been using in the x operator are actually cutting, not just deleting. When I say cutting texts, I mean that the text is deleted and it saved into a reusable place in memory all at the same time. This reusable place in memory is commonly known as the clipboard. However, Vim uses a concept called registers where you can store cut and copied text. Let's see this in action. First, I'm going to open up a terminal on my system and navigate to where I extracted the contents of the course download archive. And for me, that is Downloads vimclass. And now I'm going to open the file, cutcopypaste.txt As always, you can follow along with me now or wait for the practice exercise that follows. Let's move the first line in this file after or just below the line of hyphens. To do that, you would cut the line with a dd command. I'll just do that now, dd, this place is the cut text into what Vim calls the unnamed register. Many people call the unnamed register, the default register. If you hear me talking about the unnamed register or the default register, just know that they are the same thing. Now that you've cut the text, just move down one line, you can simply press j for example. When you press lowercase p which I'll do now, that text is placed on the line below where your cursor is. By the way, Vim refers to the p command as the put command. If it's easier for you to remember, you can think of it as the paste command. Now let's swap the next two lines that contain text in the file. To do that, you would place your cursor on the top line. Let me do that with jj, it's on the top line. And now you can press dd to cut the line, there's cut. And finally, you just press p to put or paste the line below your current position. So if you wanna memorize a command to swap lines, it would be ddp. Or you can just think of each command separately and put them together as needed. In addition to the lowercase p command, you can put or paste text with the uppercase P command. Lower case p puts the text after your cursor, uppercase P puts the text before your cursor. Let's say you wanna move the line that starts with one does not and move it just above the line that starts with its gates are guarded. To do that, just position your cursor on the line and cut it with dd. So we'll just move down here to one does not, press dd and now we have cuts the line. Next, we'll just move down to the it's gates line, j a couple of times here and the paste the cut line above this line, you use uppercase P. So I'll do Shift + P and that line was pasted above your cursor position. Even after you paste the text, it stays in the unnamed register until it is overwritten by another operation, like a delete. This means you can use it over and over if you want. To demonstrate this, let's move to the next a blank line and type P I'll just go down here, type p, that same text is put below or pasted below your cursor. You don't have to cut and paste entire lines. You can also cut and paste characters, words, et cetera. Let's move down to the line that contains fi. I'll just j down a couple of here and I'm going to press z and enter to keep my cursor on the same line yet move the text up and the view window. If you wanna swap those two characters, you would place your cursor at the character like we have here, type x to cut that character and then type lowercase p to paste that character after your cursor. Let's use this xp pattern to fix the next line, changing it from badcfe to abcdef. So we'll move down here, position our cursor here, x to cut p to paste and we'll do the same thing here, x to cut p to paste. Again, we'll swap these last two letters by x and p. Now the line is abcdef, all using the x and p key combination. Let's move down to the next line. I'll j down a couple here and let's go to the word move. I'll just this press w a couple of times here to move over there. And let's cut or delete the rest of the line with the dollar sign. Now you can put the cursor at the beginning of the line with this zero. Now to paste the text before the cursor use uppercase P. So I'll do Shift + P. One way to insert a space after this pasted text is to simply move over with the l key and enter insert mode with i and type a space and hit escape to come back to normal mode. Now, let's say that you just wanna copy text and not cut it. To do that, use the y operator, which in Vim terminology stands for yank. You can think of yank as copy. More accurately texts is being yanked into a register. Here's the language you're probably familiar with compared to the language of them Vim uses. Other editors use cut while Vim uses delete, others use copy while the Vim yanks, others paste while Vim puts. So cut, copy and paste in VimSpeak is delete, yank and put. Okay, let's get back to yanking. So you're already familiar with the operator motion pattern, using it to delete words and so on. You can use the same pattern with a y operator. So to yank or copy a word you would use yw and to yank to the end of the line, for example, you would use y$. Let's duplicate the word dupe and the next line. And to do that, we can just move our cursor to the beginning of the word dupe. I'll just j down a couple here over one to go to dupe. And now he can copy that word with yw. And let's paste it before our cursor position with capital P. Again, as you already know, you can repeat it command a number of given times by proceeding it with a number. Let's place the cursor under the beginning of the word double. Go down here and go back. I'm just going to move the text on the screen here by pressing z and enter and I'll just w over to the word double. You can copy two words by using 2yw. So let's do that now. We'll do 2yw and we should expect that double dupe is now in the unnamed register. And we can test this by pasting it with uppercase P. so Shift + P. So yes, those two words were inserted before our cursor position. Let's do it again on the next line with a slight variation of the command. So instead of repeating yw twice, you can yank two words with y2w. So we'll go down here, go to double press y2w, again, with capital P, Shift + P. And again, it does the same thing as we had done before. Just like dd deletes an entire line, yy yanks an entire line. To duplicate a line position your cursor on the line you want to copy and press yy, then you can paste it with p. So to quickly duplicate a line you can use yyp. Going to move down to the next line of text here and do what I've just described. So, yy yanks it into the unnamed register and p paste what was in the unnamed register. And now you have a duplicated line with yy to yank and p to paste. Going to position my cursor on the next block of texts and press z enter to move that text up on our screen. So it's easier to see. Just like you can delete multiple lines, you can yank multiple lines. You can see that the next paragraph is four lines long, you can yank those lines into the unnamed register with 4yy. Now paste those lines with capital P. So if I do Shift + P, those four lines or that entire paragraph is duplicated. I'm going to stop here with a yank operator. I'm not going to go over all the motions you know, just remember the operator motion pattern and you're going to be fine. Before we go any further though, I wanna show you how to use undo and repeat. Anytime you're cutting and pasting, you run the risk of making a mistake. So to undo your last change, use the u command. Going to position my cursor to the next block of text here and move it up on the screen. Now, let's say you accidentally delete the line that starts with many. Let's say you type dd and then you realize you really didn't wanna do that. Well, simply type u and your changes undone. So I'm just gonna hit u and that line returns. If you change your mind, you can redo this action with Control + r. And now if I press Control + r it redoes that last command, which is dd and deletes the line. So in short, u is undo and Control + r is redo, it's pretty much that simple. What do you think will happen if we delete two lines with 2dd and then press, u? Let's try it, we'll do 2dd. Those two lines are deleted, and now we press u and those two lines return. See it doesn't restore one line at a time as some people might think. What the u command does is totally undoes your last command, no matter how many lines in the file or how many characters it affects.

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.