Tutorial #5 - vi: searching and substituting


1. Bring up my.address in vi.  

2. Search for the string in.  Use n and N to see many places it 

occurs.

3. Search for the string ed.  Examine several occurrences of it.

4. Substitute the string IN for the first occurrence of in in each line 

in the file.  Examine the result and then change them all back to 

in.

5. Substitute the string HERE IT IS for every occurrence of C in 

the file.  Examine the result and then change them all back.

6.With the lines that include the phone number listing, change 

every occurrence of 555 to 000.

7. In the part of the text before the phone number listing, replace 

every Y that is the first character on the line with the period 

character( . ).  Then change all the periods in that part of the work 

buffer to question marks.  (You will need to quote the period when 

it is part of the search string.)

8.  Abandon the work buffer at this point.  If you want to try some 

more search and replace operations, bring your file into vi again.


If it doesn't work:

A.  The search command shouldn't give you any trouble.  If it does, 
type /, then type a string you can see on the screen and press 
Return.  If the cursor doesn't stop at the string, type N until it 
does.  A search always affects the entire file.

B. The substitution command can be tricky.  Try substituting for 
something you can see.  The command .,$:s/555/000/g should 
accomplish the number substitution.  To set the numbers back, use 
.,$:s/000-/555-/g so that you don't change any occurrences of 000 
other than those that were previously set to 555.

C. If you use an address, put it between the : and the s.  To find 
out what line number is current, type ^G to bring up the 
status line.  If you need another line number for the address, 
check its number as well.

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