## 17.22. vi Compound Searches

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

>     /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 effort, though.

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

>     /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 that the file might have the phrase "Los Alamos" all throughout 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 previous example, 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 Los Alamos is split across two lines of the file.)

Another example: a C programmer 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 (Section 18.2):

> `^M` Section 18.6

> >     :map #3 /Los Alamos/;/treasure/^M

> and repeat the search with (in this case) your F3 function key.

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.