Inserting, Changing, Replacing, and Joining

#### Goal:

The goal of this exercise is to give you practice with the different ways to insert, change, replace, and join text.

#### Instructions:

#### Open the insert-practice.txt file

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

#### Using the i command

Remember that the i command places you into insert mode right at your current cursor position. Use i to insert some text at the very beginning of the first line in the file. For example, you might insert "vim". After you have inserted the text, return to normal mode with <Escape>.

#### Using the I command

Move down to the next line of text by type 2j. Your cursor will be in the middle of this line:

<= What is your favorite color?

Insert text at the very beginning of the line by using the I command. Answer the question posed on the light. Your answer might be "blue," for example. Hit <Escape> to return to normal mode.

#### Using the a command

Position your cursor on the next line of text with 2j. Insert your name between these symbols: ><.

Enter your name here =><=

To do that, first position your cursor under > with f>. (NOTE: The f command is covered in the searching lesson. For now, know that it moves your cursor to the character you specify, but only on the same line.) Next enter insert mode after your cursor position with the a command. Now type your name and finally press <Escape> to return to normal mode.

#### Using the A command

Position your cursor on the next line of text with 2j. Append your name to the end of line by typing A which places you in insert mode at the end of the line. When you are done typing your name press <Escape> to return to normal mode.

Enter your name here:

#### Using the o command

Position your cursor on the next line of text with 2j. Enter insert mode on the line below the current line by typing o. Next, type the name of your favorite movie. When you are done press <Escape> to return to normal mode.

One the line below, type the name of your favorite movie.

#### Using the O command

Position your cursor on the next line of text with 2j. Enter insert mode on the line above the current line by typing O. Next, type "vim" and press <Escape> to return to normal mode.

^^^ One the line above, type the name of the editor you are using.

#### Using the J command

Position your cursor on the next line of text with 3j. Join the following lines:

This line belongs with the one below it.

After your edit, they will appear on the same line:

This line belongs with the one below it.

To perform this operation type J.

#### Using the R command

Position your cursor on the next line of text with 2j. Replace the work "her" with "our".

This is how the line looks before your edit.

Vim is her favorite editor.

This is how it looks after your edit.

Vim is our favorite editor.

First, position your cursor under the "h" in the word "her" by type Fh. Enter replace mode with R and type "our" followed by <Escape> to return to normal mode.

#### Using the r command

Change this line from:

I have a white car.

To this:

I have a white cat.

Position your cursor on line with 2j. Move to the "r" in the word "car" with fr. (NOTE: The f command is covered in the searching lesson. For now, know that it moves your cursor to the character you specify, but only on the same line.) Now replace the "r" with an "t" by typing r to start the replace command and then typing t to make the character replacement.

#### Using the c command

In the following sentence, change the word "great" to "brilliant":

I am having a great time in this vim class!

After your edit, the line should look like this:

I am having a brilliant time in this vim class!

First, position your cursor under "g" in the word great. You can do the by performing a forward search with /gr<ENTER>. (NOTE: The / command is covered in the searching lesson. For now, know that it moves your cursor to the next matching string you specify, even if it is on a different line in the file.) Now type cw, which stands for change word, and type "brilliant". Finally press <Escape> to return to normal mode.

In the following sentence, change "myself." to "everyone!":

I love myself.

After your edit, the line should look like this:

I love everyone!

First, position your cursor under "e" in the word everyone. You can do the by performing a forward search with /my<ENTER>. (NOTE: The / command is covered in the searching lesson. For now, know that it moves your cursor to the next matching string you specify, even if it is on a different line in the file.) Now type cW, which allows you to change a word including the punctuation that follows. Next type "everyone!". Finally press <Escape> to return to normal mode. (NOTE: You could have also used the c$ or C command in this example to achieve the same result as the cW command.

Change this entire line of text to anything you want to type:

Type something wonderful here.

First, place your cursor anywhere on the line with 2j. Next, use the command cc which allows you to change the entire line. Type anything you want. For example, "The sky is beautiful!". Finally press <Escape> to return to normal mode.

#### Changing case with the ~ command

Capitalize the first letter of the word "monday" on this line:

monday <= The "m" is supposed to be in uppercase.

To do that place your cursor under the "m" by performing a forward search with /m<ENTER>. Next type ~ to perform the case switch case operation.

Capitalize the entire word "shout" on this line:

Don't shout. It's not nice.

To do that place your cursor under the "s" by performing a forward search with /sh<ENTER>. Next type g~w to perform the case switch case operation on the word motion.

Switch the case of the entire following line:

mONDAY'S START BETTER WITH coffee.

After your edit it will look like this:

Monday's start better with COFFEE.

To make that change, place your cursor anywhere on the line with 2j. Next type g~~ to switch the case of the entire line.

#### Using the U command

On the following line, change the word "Shout" to "SHOUT":

Don't Shout. It's just too loud.

To do that place your cursor under the "S" by performing a forward search with /S<ENTER>. Next type gUw to perform the uppercase operation on the word motion.

Try it again on the next line, but this time use gUW.

#### Using the u command

One the following line, change the word "Whisper" to "whisper":

Please Whisper.

To do that place your cursor under the "W" by performing a forward search with /W<ENTER>. Next type guw to perform the lowercase operation on the word motion. (NOTE you could have also just used the ~ in this case.)

#### Repeating commands

One line below the following line, create a new line that contains 80 asterisks (*).

Create a line of asterisks below:

To do that place your cursor one line below it. You can use 3j, for example. Now enter insert mode with a count of 80 by typing 80i. Next type * to insert an asterisk. Finally press <Escape> to return to normal mode and watch vim insert that asterisk 80 times for you.

One line below the following line, create 3 new lines that contain a hyphen (-).

Create 3 lines that begin with "-" below:

To do that place your cursor on the line by typing 2j. Now use the o command with a count of 3 typing 3o. Next type - to insert a hyphen on the new line. Finally press <Escape> to return to normal mode and watch vim insert that line 2 more times for you.

#### 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 inserting, changing, replacing, and joining skills!

#### Exit out of vim

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