image
Exercise: Cut, Copy and Paste

Contents

Start course
Difficulty
Intermediate
Duration
30m
Students
191
Ratings
5/5
starstarstarstarstar
Description

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.

Prerequisites

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

Resources

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

 

Transcript

The goal of this exercise is to give you practice with cut, copy and paste operations in Vim. Additionally, you'll get a chance to work with Vim's registers. So the first thing you wanna do is start a command line session on your local machine, and then next use Vim to open the dyp.txt file that came in the course downloads. And of course, to do that you need to navigate to the location of that file. And remember, this could be slightly different for you depending on where you extracted the contents of the course download. For me, it's in my Downloads folder. So I'm going to cd into the Downloads directory. And then from here I'm going into the Vimclass directory and finally I'm going to start Vim with vim dyp.txt which by the way, stands for delete, yank, and put. Let's start out by deleting the first line of the file by typing dd. This command places that line into the default register. Now let's put that line below this new first line here with the p command. So I will hit p and that original first line is now pasted below. Now, remember that you can reuse the contents of the default register. So let's place the original first line of the file just below the one that starts What was the first line in the file originally? So I'll just hit j a couple of times and then hit p to paste that line again from the default register. Now let's put that original first line in the file just above this next line here, that says, What was the first line in the file originally? place it above: So I'll just hit J a couple of times to position my cursor on that line hit Shift + p and it will paste above that line again from the default register. Go ahead and move down the file here a couple of lines with j and place my cursor over the e in this misspelled version of the word the. And I don't know about you but I happen to make these typing mistakes all the time. So it comes in handy to know a shortcut to swap letters like this. And to do that what we can do is press the x key to delete the character and then hit p to put after our current position. And so that effectively just swaps the letters. And so we're going to do this with these other words. This should be spell not psell, so we'll do x and p and now that word says spell, we'll do it down here, Vmi should be Vim. Word again, we can swap the r and o with x and p and this misspelled version of that, we'll place our cursor over the a and do x and p to swap those characters, so that the word that is now spelled correctly. Just position my cursor here on this next line I'm going to hit z + Enter to move the text up in our view window here. And now what we wanna do is change this line from reading second, first, third to first, second and third. And to do that, we can just our cursor at the beginning of the line under s in the word second. Now we can delete it along with a comma that follows that word with dW. Notice that we're using W here remember that that uses space as word boundaries. So it deleted the comma like we wanted to do. And so next, what we can do is position our cursor at the beginning of the word third we can just do this with Shift + W to jumped over that comma and place this right where we need to be. Now we can put the text in the default register before our current cursor position with a P command. So I'll type Shift + P and now our sentences ordered first, second third like we wanted it to be. Let's go down to this next line and duplicate it. One way to do that is yy to quickly yank it into the default register and then p to paste it below our current cursor line. So that effectively duplicates the line. Now let's move down to this next bit of text. And what we wanna to do is duplicate the words really really. So let's place our cursor under the r and the first word here and next we'll yank those two words including the punctuation into the default register with y2W. Now we can paste them before our current position with P. So we'll do shift + P. Now the line reads, I really, really, really really love vim, which of course I do and I hope you do too. So, next what we wanna do is practice using the numbered registers. So let's put the text to do above any lines in the file that start with fix this. Also, what we wanna do is delete any lines that read delete this. So we're gonna start at our current cursor position and work our way down through the file, mixing these deletes and puts. So we'll go to the to do line with a couple of js. We'll yank it with yy. Next we'll go down a couple of lines with jj to the first instance of delete this and we can delete those two lines with two dd. Now we're going to move down one line to position our cursor on the line that reads fix this. And what we wanna do is paste to do above it. And to do that, you'll need to use the zero register which contains the most recent yanked text. So we can type double quote zero P. Now, what we're going to do is repeat this process a couple more times. So, we have three lines that we need to delete. So we'll do 3dd, place our cursor on the fixed this line, use double quote zero shift + P to paste from the zero register. And we'll delete these two lines with two dd. Again, double quotes zero, shift + P and then three delete and then again, double quote, zero shift + P and finally to dd to finally get everything looking the way we want it. Let's position our cursor on the next line with j and I'm just going to hit the z + Enter to move the text up on our screen so it's easier to read. Now, what I wanna do is place this into the J register. So we can do that with double quote jyy. Likewise, the next line we're going to put in the f register moved down with J double quote fyy. Now we'll moved down here and lets put the contents of the j register below and we can do that with double quote jp and now the contents of the J register is pasted below. We'll do the same thing with the f register. Go down a couple of lines, double quote fp and what was in the f register is now pasted below that line that our cursor was on. Next, what we wanna do is append this line to the j register and to do that we just place our cursor anywhere on the line do double quote shift + J and then yy to append to the j register. Likewise, we'll move down one line here and append this line to the f register. So we'll do double quote capital fyy. Now let's move down here, a couple of lines and paste the contents of the j register. So we'll do double quote, lowercase j + p. And this time you can see that the additional line was pasted because we appended it to the original contents of the j register. Again, we'll do the same thing the f register. I move down a couple of lines, double quote fp. We already know what's in the j and f registers because we pasted them on our screen but if you wanted to practice using the register command you could use colon reg and then specify the j and f registers to view their content. So I'll hit enter here and we can see the contents of those registers there. I'll hit enter to continue. Now, let's practice the undo and redo commands. Let's just position our cursor down here on the line that begins at one. I'm just going to hit z + Enter to move this up the screen and what I'm going to do is type three dd to delete all three of those lines. Now let's undo that deletion by typing you and notice that all three of the lines return. Again you or undo undoes the last command, no matter how little or how much text is affected in your file. Now, what we can do is say, redo the command. Let's say, well I decided I really wanted to remove those lines so I can type control + r and 3 dd is executed again, redoes that command and those three lines are removed. Let's practice this with the insert command. Let's get into insert mode with i and then let's just type a sentence. Let's say Vim is fun. And now we'll hit esc to return to normal mode. And let's say, we decide that we really didn't want to add that text. Well, we can undo that entire edit with u. So that entire insert command is undone. Again, if we want to put it back, we can just type control + r and that insert mode edit is replaced. At this point I just want to encourage you to experiment and come up with some of your own practice exercises. But again if you have a file that needs editing, go ahead and open it up and Vim and try some of your delete, yank and put skills. When you're done and if you want to try this exercise again sometime in the future, you can quit without saving with colon :q!

About the Author
Students
13927
Courses
61
Learning Paths
13

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 Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.