home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.26 Shell Escapes: Running One UNIX Command While Using Another Chapter 30
vi Tips and Tricks
Next: 30.28 Keep Track of Functions and Included Files with ctags and tags
 

30.27 vi Compound Searches

You probably know that you can search for a word or phrase with the vi command / (slash):



/treasure

If you have a file that uses the same word over and over again, you might want to find one particular place that the word is used. You can repeat the search with the n command until you find the place you want. That can take time and work, though.

For example, suppose you want to find the word "treasure" in the sentence that has words something like "Los Alamos residents...treasure," but you can't remember exactly how the sentence is written. You could use wildcards in your regular expression ( 26.4 ) :



/Los Alamos.*treasure

but then the phrases "Los Alamos" and "treasure" have to be on the same line of the file you're searching - and they won't always be. Also, you want your cursor on the word treasure , but that search would put the cursor on Los instead.

"Hmmm," you say, "How about two separate searches, like this?"

/Los Alamos
/treasure

The problem there is: the file might have the phrase "Los Alamos" all through it; you might have to type n over and over until you get to the sentence with treasure .

Here's the easy way: a compound search. Say your cursor is on line 1 of the following file:

Before the second World War, there was a treasured boys' school in
what was to become the city of Los Alamos, New Mexico. The school at
Los Alamos changed the lives and made a lifelong impression on most boys
who attended. One of the boys who attended the Los Alamos school went on
to propose that remote set of mesas as a site for the U.S. Government's
   ...
Since the war ended, most of the boys' school ranch buildings have been torn
down or replaced. But there's one building that Los Alamos residents still
use and treasure. It's The Lodge, a log building on the edge of what's now
   ...

Type the command:



/Los Alamos/;/treasure/

That means "find the first occurrence of treasure just after Los Alamos ." Starting at the top of the example above, that search will skip past all the treasure and Los Alamos words until it finds the word treasure on the last line shown. (It's probably smarter to type just /Alamos/;/treasure/ in case the Los Alamos is split across two lines of the file.)

Another example: a C programmer who wants to find the printf function call just after the line where i is incremented by two ( i += 2 ). She could type:



/i += 2/;/printf/

NOTE: You can't repeat a compound search by typing n . The easiest way is to define the search as a key map ( 31.2 ) :



^M
 



:map g /Los Alamos/;/treasure/^M

and use (in this case) g to repeat the search.

- JP


Previous: 30.26 Shell Escapes: Running One UNIX Command While Using Another UNIX Power Tools Next: 30.28 Keep Track of Functions and Included Files with ctags and tags
30.26 Shell Escapes: Running One UNIX Command While Using Another Book Index 30.28 Keep Track of Functions and Included Files with ctags and tags

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System