Inserting, Changing, Replacing, and Joining: Goal
Inserting, Changing, Replacing, and Joining: Goal
Goal:
The goal of this exercise is to give you practice with the different ways to insert, change, replace,
and join text.
Instructions:
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
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>.
Move down to the next line of text by type 2j. Your cursor will be in the middle of this line:
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.
Position your cursor on the next line of text with 2j. Insert your name between these symbols: ><.
http://www.LinuxTrainingAcademy.com
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.
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.
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.
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.
Position your cursor on the next line of text with 3j. Join the following lines:
http://www.LinuxTrainingAcademy.com
To perform this operation type J.
Position your cursor on the next line of text with 2j. Replace the work "her" with "our".
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.
To this:
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.
http://www.LinuxTrainingAcademy.com
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.
I love myself.
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.
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.
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.
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.
To make that change, place your cursor anywhere on the line with 2j. Next type g~~ to switch the
case of the entire line.
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.
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 (*).
http://www.LinuxTrainingAcademy.com
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 (-).
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!
If you want to abandon your changes so you can try this practice exercise again, use :q!<ENTER>.
http://www.LinuxTrainingAcademy.com