Searching the file for specific text



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

You can instruct vi to search for a line that contains a particular "string" (sequence of characters) and then move your window to show the area around that line.

Use the command

/abcd<CR>

where abcd is any sequence of characters, to search "down" through the file for the next line that contains abcd.

Search expressions can actually be regular expressions, which allow you to match patterns in addition to exact matches, as described below.

Be careful of regular expression metacharacters. Escape them by preceding with a backslash if you want to treat them as normal characters. When in doubt whether a character has a special meaning, it never hurts to precede it with a backslash (use two backslashes in a row to get the backslash character itself).

Use the command

?abcd<CR>

to search "up" through the file for the next line containing abcd.

Vi remembers your last search command. To continue the search for the next occurrence, type a single n. To continue the search in the opposite direction from that in which it started, type N.

Vi will wrap around the file when searching. When it reaches the bottom, it starts over again at the top, and vice-versa.

Comments or Questions?