3.3 Movement by Searches[/] One of the most useful ways to move around in a large file quickly is by searching for text, or more properly, a pattern of characters. Sometimes a search can be performed to find a misspelled word or to find each occurrence of a variable in a program.
The search command is the special character
/ pattern A pattern can be a whole word or any other sequence of characters (called a "character string"). For example, if you search for the characters red , you will match " red " as a whole word, but you'll also match "occur red ". If you include a space before or after pattern , the spaces will be treated as part of the word. As with all bottom-line commands, press [RETURN] to finish. ( vi , like all other UNIX editors, has a special pattern-matching language that allows you to look for variable text patterns; for example, any word beginning with a capital letter, or the word The at the beginning of a line. We'll talk about this more powerful pattern-matching syntax in Chapter 6, Global Replacement . For right now, think of pattern simply as a word or phrase.) vi begins the search at the cursor and searches forward, wrapping around to the start of the file if necessary. The cursor will move to the first occurrence of the pattern. If there is no match, the message "Pattern not found" will be shown on the status line. Using the file practice , here's how to move the cursor by searches:
To begin a search backward, type a
In this case, the search wraps around to the end of the file, if necessary. 3.3.1 Repeating Searches[n] The last pattern that you searched for stays available throughout your editing session. After a search, instead of repeating your original keystrokes, you can use a command to search again for the last pattern.
n
,
N
,
/
or
?
.
The direction of your search (
/
is forward,
?
is backward) is displayed
at the bottom left of the screen.
To continue with the example above, since the pattern scr is still available for search, you can:
wrapscan
,
that controls whether searches wrap.
You can disable wrapping like this:
:set nowrapscan
When
Address search hit BOTTOM without matching pattern
When This section has given only the barest introduction to searching for patterns. Chapter 6 , Global Replacement , will teach more about pattern matching and its use in making global changes to a file. 3.3.2 Current Line Searches
[f]
There are also miniature versions of the search commands that
operate within the current line.
The command The in-line search commands are summarized below. None of these commands will move the cursor to the next line.
| ||||||||||||||||||||||||||||||||||||||||||
|