Cut, Copy, and Paste (Delete, Yank, and Put)

#### Goal:

The goal of this exercise is to give you practice with cut, copy, and paste operations. Additionally you'll get a chance to work with registers.

#### Instructions:

#### Open the dyp.txt file

First, start a command line session on your local machine. Next, use vim to open the "dyp.txt" file that came in the course downloads. To do that, navigate to the location of the file. Remember this could be different for you depending on where you extracted the contents of the file. This example assumes the course download was saved into your Downloads folder and extracted from there.

cd Downloads cd vimclass vim dyp.txt

#### Swap the first two lines of the file

First, delete the first line of the file by typing dd. This places the line into the default register. Now put the line below the new first line in the file with the p command.

Here is what the first two lines of the file look like before your edit:

This was originally the first line in the file. This was originally the second line in the file.

Here is what they look like after:

This was originally the second line in the file. This was originally the first line in the file.

#### Put the first line in another place in the file

Remember that you can reuse the contents of the default register. Place the original first line of the file just below this line:

What was the first line in the file originally? Place it below:

To do that, position your cursor on that line and use the put command by typing p.

#### Practice putting lines above your cursor position

####

Put the original first line in the file just above this line:

What was the first line in the file originally? Place it above:

To do that, place your cursor on that line and type p.

#### Fix spelling/typing mistakes by swapping characters

Position your cursor under the "e" on this line:

teh

Swap the "e" and "h" so that the word reads "the". To do that, type x to delete the "e" and p to paste it after your current cursor position.

Use the same process to fix the spelling or typing mistakes on the following four lines:

psell = spell

vmi = vim

wrod = word

taht = that

#### Swap words

Change this line from:

second, First, third.

To:

First, second, third.

To do that position your cursor at the beginning of the line under the "s" in the word second. Delete it along with the comma that follows with the dW command. (Notice the capital W). Next, position your cursor at the beginning of the word "third" by typing W. Now put the text in the default register before your cursor position with the P command.

#### Duplicate a line

Duplicate the following line, placing another copy of it just below it:

Duplicate this line.

Position your cursor on the line and yank it into the default register with yy. Next, paste it below the line with the p command.

#### Duplicate a word

Duplicate the words "really, really," on the following line:

I really, really, love vim!

Place your cursor under the "r" in the first occurrence of the word "really". Next yank those two words, including the punctuation, into the default register with y2W. Next, paste them before your current cursor position with P. The line should now read:

I really, really, really, really, love vim!

#### Use the numbered registers

Put the text "TODO" above any lines in the file that start with "Fix this". Also, delete any lines that read "Delete this". Start at your current cursor position and work your way down through the file mixing your deletes and puts.

First, yank the line that reads "TODO" with yy. Next delete the two lines that read "Delete this" with 2dd. Position your cursor on the line that reads "Fix this" and paste "TODO" above it. To do that you'll need to use the "0 register which contains the most recent yanked text. Type "0P. Repeat this process until all the "Fix" lines have "TODO" above them and all the "Delete" lines are deleted.

#### Use named registers

Place the following line in the "j register:

Yank this line into the "j register.

Position your cursory anywhere on the line and type "jyy.

Now place the following line in the "f register.

Yank this line into the "f register.

Position your cursory anywhere on the line and type "fyy.

Next put the contents of the "j register below this line with "jp:

Put the contents of the "j register below:

Next put the contents of the "f register below this line with "fp:

Put the contents of the "f register below:

Append the following line to the "j register:

Append this line to the "j register.

Place your cursor anywhere on the line and type "Jyy. (Notice the capital J).

Append the following line to the "f register:

Append this line to the "f register.

Place your cursor anywhere on the line and type "Fyy. (Notice the capital F).

Look at the contents of all the registers with :reg<ENTER>. If you only want to view the "j and "f registers, use :reg jf<ENTER>

Now put the contents of the "j register below this line with "jp:

Put the contents of the "j register below:

Next put the contents of the "f register below this line with "fp:

Put the contents of the "f register below:

#### Undo and Redo

Delete the following three lines:

ONE) TWO) THREE)

You can do that by positioning your cursor on the first line and typing 3dd. Undo your deletion by typing u. Notice how all three of the lines return.

Redo your command with Ctrl-r. Now the three lines are removed again.

Insert a new line into the file. First enter insert mode with i. Now type any sentence into vim. For example, you could use this sentence:

Vim is fun!

Press <Escape> to return to normal mode. To undo your text insertion type u. Now redo your text insertion with Ctrl-r.

#### Your turn

I encourage you to experiment and come up with some of your own practice exercises. Better yet… Do you have a file that needs editing? Open it up in vim and try using some of your delete, yank, and put skills!

#### Exit out of vim

If you want to abandon your changes so you can try this practice exercise again, use :q!<ENTER>.