McMullin Questions Chapter 9

9.1 Starting and Quitting vi

9.1.1 Start vi
At the shell prompt, enter the command: vi 
a) What is the name of the file you are editing? 
b) What do the "~" characters represent?
c) What mode are you in?

Enter the following command: :q
d) What happened?

9.1.2 Add Text
Start vi again.  Type the command: a
a) What mode are you in?

Type the following line; press Enter at the end of the line.
	Memo: Everybodyxx
	
b) What does the a command do?
c) Backspace over the "xx".  What happens?
d) Press the Escape key (<ESC>). What does it do?
e) Press the Escape key (<ESC>) again. What does it do this time?

9.1.3 Save the File

Enter the :q command.
a) What happens?

Enter the :w command.
b) What happens?

Enter the :w memo command.
c) What message does vi display?

Using the a command, add the following text:
Congratulations to Chris, the former bachelor.
Press the Escape key (<ESC>) and enter the following command: :w

d) What name is the file saved under this time?

9.1.4 Exit vi

Using the a command, add the following text:
Chris is now the proud father of a little boy.
The early arrival (early this morning, two weeks early)
means that Chris won't be in the office this week or next week.

Best wishes from all of us!
Press the Escape key (<ESC>) and enter the following command: :q
a) What happens when you quit this time?

Enter the following command: :wq
b) What happens?

Open the file for editing again (Use the command vi memo)
Using the a command, add some text of your own choice:
Now press the Escape key (<ESC>) and enter the following command: :q!
c) What is the difference between the :q command and the :q! command? 
(You can use the cat) command to examine the memo file)

9.1.5 True or False
1) The vi editor has modes.
2) Pressing the Escape Key twice leaves you in the same mode you started in.
3) The :w command is short for :write, and :q is short for :quit, so :wq is short for :writequit

9.1.6 Multiple Choice
4) When you open a file in vi, which of the following does it show you?
a)  The file's name (if it already exists).
b)  The file's size (if it already exists)
c)  Where the lines of the file end, by filling the rest of the lines with '~'
d)  All of the above.

5) Which of the following is true of commands that start with a ":"?
a) They must be completed by pressing Enter.
b) They are not displayed on the screen.
c) They are part of vi, not ex
d) They are always short.

9.2 Inserting Text

9.2.1 Append Text
If the file "todo" doesn't exist, create it using the following procedure:
$ vi todo
Use the a command to create the following text:
To Do:

Pay fine $99.32
Coat to dry cleaners [open 9-6]
* Buy oats
Friday: Take out garbage
Saturday: Paint garage door, if dry
* Sat: Show kids goat at zoo

Press the Escape key (<ESC>) then type: :w
Now type the command: a

a) What happens to the cursor? 
b) What mode are you in now?

Type the following letters:
hings t
c) Where are the letters displayed?

Press the Escape key (<ESC>). 
d) What mode are you in now?

9.2.2 Insert Text
Type the following command: i
a) What happens to the cursor?
b) What mode are you in now?

Type the following letters (end with a space):
I have 
c) Where are the letters displayed?

Press the Escape key (<ESC>) then type: I
Type the following letters (end with a space):
Ten
d) Where are the letters displayed?

Press the Escape key (<ESC>) then type: A
Type the following letters (start with a space):
 Today
e) Where are the letters displayed?

9.2.3 True or False

1) The cursor may change shape to tell you you're in input mode.
2) Each of the commands to enter text input mode inserts 
the text in a different place.

9.2.4 Matching
Match commands with the location where text is inserted.
1) i		i) Beginning of line
2) A		ii) End of line
3) I		iii) After the cursor
4) a 		iv) Before the cursor

9.3 Moving The Cursor

9.3.1 Move the Cursor on the line
In command mode, type the following commands:
$ 
h 
^ 
l 

a) What do each of these commands do?
b) What happens when you try to use l to go past the end of a line?

9.3.2 Move the Cursor through the file
In command mode, type the following commands:
j 
k 
L 
H
M 

c) What do each of these commands do?

Add about 40 lines of text; The content is of your choice. e.g.
Hold down the enter key until all text has disappeared from view, 
then press the Escape Key to go back into command mode.

Type the following commands:
G
1G
d) Where does the cursor go with each command?

Press the <CTRL-G> key combination.

e) What information is displayed?

Go back to the beginning of the file.

Press <CTRL-F> and then <CTRL-B>

f) What do these commands do?

9.3.3 Move the Cursor by Context
Use the 1G command to return to the top of the file.
Type the following commands:
www
b

a) What happens each time you press w? 
What happens when you press b?

b) What happens when you get to the end of a line?

9.3.4 Move the Screen Display
Use the j command to move the cursor 
to the middle of the screen. Type the following commands:
<CTRL-Y>
<CTRL-E>
z

a) What does each command do?

9.3.5 True or False

1) For all of the movement commands described in this section 9.3, 
starting them with a number causes them to be repeated that many times.

2) The <CTRL-G> command displays information 
about a file.

9.3.6 Matching

3) In the following table, Match the command to its movement
a) i		i) one character to the right
b) k		ii) one character to the left
c) l		iii) one line up
d) h		iv) one line down

4) In the following table, Match the command to its movement
a) <CTRL-B>		i) Cursor moves one screen forward
b) <CTRL-E>		ii) Cursor moves one screen backward
c) <CTRL-F>		iii) Screen moves one line up, cursor fixed
d) <CTRL-Y>		iv) Screen moves one line down, cursor fixed

9.3.7 Multiple Choice

5) Which command moves to the end of the file? 
a) <CTRL-G>	
b) $
c) G
d) <CTRL-L>

9.4 Deleting Text

9.4.1 Delete a line
With the cursor on the first line, type the following commands:
dd 
5dd

a) What did the dd command do?

b) What effect does the 5 have on the command?

Move to the line after "Best Wishes"

Enter the following command:
:.,$d
Exit the editing session without saving the changes.

c) What does the above command do?

9.4.2 Delete a Word or Character
Use vi to open the file memo again.
Move the cursor to the line beginning with: "Memo:".
Type the following command:
dw 

a) What does this command do?

Type the following command:
dl 

b) What does this command do?

Type the following command:
x 

c) What does this command do?

Type the following command:
d 
Exit without saving the changes.

d) What does this command do?

9.4.3 Write Over Text
Use vi to open the file memo again.
Move the cursor to the "b" in the line ending with: "little boy".
Type the following command:
rB 

a) What does this command do?

Go back to the beginning of the line and then type the following command:
RAttention! 

b) What does this command do?

c) What happens when you press Enter and continue typing text?

9.4.4 Undo a Change
Press the Escape Key and then type the following command:
u 

a) What does this command do?

b) What happens when you type the u command twice?

9.4.5 True or False

1) The command to delete a line is D
2) A command that can be followed by a movement (like d) affects
the entire current line if you double it
3) Giving the undo command four times is the same as not giving it at all.

9.4.6 Matching

4) In the following table, Match the command to its movement
a) 4dd			i) Delete from current line to the end of file
b) dG			ii) Replace letter under the cursor with "q"
c) d$			iii) Delete current line and 3 lines below it
d) rq			iv) Delete to the end of the current line

5) In the following table, Match the command to its movement
a) Delete letter under cursor		i) u
b) Replace text and enter input mode	ii) x
c) Undo last change			iii) dh
d) Delete current line and line above	iv) R

9.5 Saving a File

9.5.1 Save The File Under a New Name
Open the file memo and enter the following commands:
:w newmemo 
:w

a) What name is the file saved under the first time? The second time?

Enter the following command:
:w newmemo 

b) What happens?

Enter the following command:
:w %
:w! #

c) What happens?

9.5.2 True or False

1) Even if you save a file under a new name, memo 
keeps using the original name.
2) To write over an existing file that isn't being edited, use the ! after 
the :w command

9.5.3 Matching

3) Match the character with its meaning in a colon command
a) !			i) The Alternate file name
b) #			ii) The Current file
c) %			iii) Do not check; do it anyway.

9.6 Searching Text

9.6.1 Find a character on the line
Open the file memo.  Move the cursor to line 6
(The line beginning, "The early") and type the following command:
fe 

a) What does this command do?

Type the following command:
2fe

b) What effect does the 2 have?

Type the following command:
Fe

c) How is F different from f?

9.6.2 Search for a Regular Expression
Enter the following commands:
/y 
?y

a) Which command searches forward in the text?  
Which command searches backwards?

b) What happens when you try to search past the end 
(or beginning) of the file?

c) Does vi use basic regular expressions 
or extended regular expressions?

9.6.3 Search for a Word beginning or Word ending
Enter the following commands:
/y\>

a) What does the "\>" expression match?

9.6.4 Repeat a Search
Type the following command
n

a) What does this command do?  What direction does it search?  What does it search for?

Enter the following commands:
/
?

b) What do these commands do? What direction do they search?  What do they search for?

c) What do you think would happen if you used / as the very first search on opening a file?

9.6.5 True or False

1) vi uses basic regular expressions, with some additions.
2) The command //z repeats the last search and puts the 
   matching line on the first line of the screen.
   

9.6.6 Matching

3) Match the command with what it does:
a) /\<the\>	i) Search forward for the word "the"
b) ?^[^A-Z]	ii) Repeat the last search
c) n		iii) Search backward for a line not beginning with Capital letters
d) /[A-Z]$	iv) Search forward for lines ending in a Capital letter

4) In the following table, Match the command to its movement
a) Delete from cursor to first ;	i) fG
b) Search left on line for "G"		ii) dt;
c) Delete from cursor up to but 
not including first ;			iii) df;
d) Search right on line for "G"		iv) FG

9.7 Searching and Replacing Text

9.7.1 Replace the 1st Occurrence on the current line
Open the file memo.  Move the cursor to line 6
(The line beginning, "The early") and type the following command:
:s/early/late/ 

a) What does this command do?

b) What happens if you repeat the command?

c) What happens if you enter just :s and press Enter?

9.7.2 Replace All Occurrences on the current line
Move the cursor to line 6 (The line beginning, "The late") and type the following command:
:s/late/early/g 

a) What happens?  What effect does the g have on the command?

9.7.3 Replace a word throughout the file
Type the following command:
:%s/boy/girl/ 

a) What happens?  

Enter the following command:
:%s/Chris/&stopher/g

b) What happens?  What special meaning does "&" have in the replacement string?

9.7.4 True or False

1) The :s is short for substitute.
2) Each repetition of the :s command replaces the first
occurence of the pattern.

9.7.5 Matching

3) Match the command with what its results:
a) :s/A/z/g		i) "Aardvark becomes "AArdvark"
b) :s/\(A\)/\1\1/	ii) "Aardvark becomes "AAardvark"
c) :s/[Aa]/&&&/g	iii) "Aardvark becomes "zardvark"
d) :s/\(A\)a\(rdvark\)/\1\1\2/	iv) "Aardvark becomes "AAAaaardvaaark"

9.8 Moving Text

9.8.1 Move a line of text
Enter the following commands:
4G
4dd
j
p

a) What do each of these commands do?

b) What does the p command do?

c) Try it again but use the command: P; How is it different?

9.8.2 Copy Text
Go to the 1st line of the file and Enter the following commands:
yy
p

a) What do each of these commands do?

Enter the following commands:

:1,2w tmpfile
:r tmpfile

b) What do these commands do?

9.8.3 True or False

1) The p command puts deleted or yanked text back into the file.
2) The y command behaves like the d command, except it doesn't
delete text, it only copies it.
3) The :r command inserts a file into the buffer.

9.9 Perhaps harder questions

Use the file called verse for the following questions:
$ cat verse
12 botles of milk on teh wall
12 botles of milk
There'll be 11 botles of mikl on teh wall
If one of those botles should happen to fall

1) What commands would be required to correct the errors 
and sort the lines so it reads:
12 bottles of milk on the wall
12 bottles of milk
If one of those bottles should happen to fall
There'll be 11 bottles of milk on the wall

2) How would you go about creating the file song, 
which contains 15 verses for each number of bottles 
from 15 to 1?


Questions? Robert Katz: rkatz@ned.highline.edu
Last Update July 2, 2002