Deleting Text

#### Goal:

The goal of this exercise is to practice deleting text in a file. You'll also practice the [count][operator]{motion} pattern.

#### Instructions:

#### Open the practicedeleting.txt file

First, start a command line session on your local machine. Next, use vim to open the "practicedeleting.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 practicedeleting.txt

#### Practice deleting individual characters

Move to the 3rd line of the file. Remember, you can do this in a few different ways. You can press the j key until you're there, you can use 3gg, 3G, or :3<ENTER>.

Remove the extra "k" in the word "mistakke" by positioning your cursor under one of the additional letters and hit the x key. Here is what the sentence looks like before your edit:

First, fix this spelling mistakke.

Here is what it looks like after:

First, fix this spelling mistake.

Move to the 4th line of the file. Remove the repeated letters from each of the words. Use x to delete the character under your cursor to do so. Here is what the sentence looks like before:

Fixx theese allso.

After you delete the additional "x", "e", and "l", it will look like this:

Fix these also.

Move down to the next line:

Delete this text with the X command.

Position your cursor at the end of the line. You can repeatedly press the l key, or if you want to be really efficient you can use the $ key. Now delete all the text you can with X key. You'll find that only the period remains on the line:

.

To delete that character, press x. Now the line is empty.

#### Practice deleting motions

Move down to the next line:

Who let the dogs out? cats

Position your cursor one character right of the question mark. Your cursor will be in the space between "?" and "cats". Delete the remaining text on the line. You can use d$, or an even shorter version of the command D. After your edit, the line will look like the following:

Who let the dogs out?

Now position the cursor at the beginning of line 43. (Hint: 43gg works.) Delete the first word of the line with the d operator. Remember the pattern of [count][operator]{motion}. To delete that first word you can use dw or even dW. Here is the line before the first word is deleted:

Far far away, behind the wild mountains, far from the countries Vokalia and

Here is the line after the first word is deleted:

far away, behind the wild mountains, far from the countries Vokalia and

Delete the second word, too. Now the line looks like so:

away, behind the wild mountains, far from the countries Vokalia and

Delete the text "away, " with two keystrokes. Remember that the w motion will stop at punctuation while the W motion ignores punctuation and stops the cursor after white space. So, to delete "away, " in two keystrokes you use dW. Now the line looks like this:

behind the wild mountains, far from the countries Vokalia and

Position your cursor at the beginning of the first occurrence of the word "the". Delete the first word in the sentence using an operator and a motion. To do that, type db. You could have also used dB. Here is how the line appears now:

the wild mountains, far from the countries Vokalia and

Now, delete words "the wild ". One way to do this is to use 2dw.

mountains, far from the countries Vokalia and

Now delete "mountains, far ". A motion that moves your just past "mountains, far " is 2W. So, use d2W to delete the text. This is what remains on the line, now:

from the countries Vokalia and

#### Practice deleting lines

Delete the line by using dd. Your cursor is now placed on this line:

Consonantia, there live the blind texts. Separated they live in Bookmarksgrove

To delete multiple lines use, [count]dd. Let's delete these lines:

Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.

Notice that there are 3 lines in total. One line begins with "Consonantia", the next with "right" and the third line is blank and doesn't have any text at all. To delete those 3 lines, use 3dd. Now your cursor is placed on this line:

A small river named Duden flows by their place and supplies it with the

Delete the next three lines by using one keystroke. Simply type a period and the previous command will be repeated. When you press . the following three lines will be deleted:

A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.

#### Save your work (or not!)

If you want to save your changes and keep vim running, you can use :w<ENTER>. To save your changes and immediately exit, type :wq<ENTER>. If you want to abandon your changes, use :q!<ENTER>. It's up to you.

#### Your turn

If you're up to it, have some fun deleting text in the file using what you know.