< Previous | Contents | Next >
Searching The Entire File
To move the cursor to the next occurrence of a word or phrase, the / command is used. This works the same way as we learned earlier in the less program. When you type the
/ command a “/” will appear at the bottom of the screen. Next, type the word or phrase to be searched for, followed by the Enter key. The cursor will move to the next location containing the search string. A search may be repeated using the previous search string
with the n command. Here's an example:
The quick brown fox jumped over the lazy dog. It was cool. Line 2
Line 3
Line 4
Line 5
The quick brown fox jumped over the lazy dog. It was cool. Line 2
Line 3
Line 4
Line 5
Place the cursor on the first line of the file. Type:
/Line
/Line
followed by the Enter key. The cursor will move to line 2. Next, type n and the cursor will move to line 3. Repeating the n command will move the cursor down the file until it runs out of matches. While we have so far only used words and phrases for our search patterns, vi allows the use of regular expressions, a powerful method of expressing com- plex text patterns. We will cover regular expressions in some detail in a later chapter.