OpenWindows Advanced User's Guide

6.7 Searching and Replacing with vi

vi provides several ways to find your place in a file by locating a specified string of characters. It also has a powerful global replacement function.

6.7.1 Finding a Character String

A character string is simply one or more characters in a row. It may include letters, numbers, punctuation, special characters, blank spaces, tabs, or carriage returns. A string may be a grammatical word or it may be part of a word.

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string "meta", type /meta followed by Return.

Type n to go to the next occurrence of the string; type N to go to the previous occurrence.

To search backward in a file, you can use ? instead of /. In this case, the directions of n and N are reversed.

Searches normally are case-sensitive: a search for "china" will not find "China." If you want vi to ignore case during a search, type :set ic. To change it back to the default, case-sensitive mode, type :set noic.

If vi finds the requested string, the cursor will stop at its first occurrence. If the string is not found, vi will display Pattern not found on the last line of the screen.

Certain special characters ( / & ! . ^ * $ \ ?) have special significance to the search process and must be "escaped" when used in a search. To escape a special character, precede it with a backslash (\). For example, to search for the string "anything?" type /anything\? and press Return.

These special characters can be used as commands to the search function, so if you want to search for a string including one or more of these characters, you must indicate this by preceding the character with a backslash. To escape a backslash itself, type \\.

6.7.2 Refining the Search

You can make searches more precise by tagging the string with indicators for the following characteristics:

To match the beginning of a line, start the search string with a caret (^). For example, to find the next line beginning with "Search", type:


To match the end of a line, end the search string with a dollar sign ($). For example, to find the next line ending with "search.", type:


(Note that the period is escaped with a backslash.)

To match the beginning of a word, type \< at the beginning of the string; to match the end of a word, type \> at the end of the string. Thus, to match a word, rather than a string, combine the end-of-word and beginning-of-word tags in the search pattern. For example, to find the next occurrence of the word--as opposed to the string--"search", type:


To match any character, type a period (.) in the string at the location to be matched. For example, to find the next occurrence of "disinformation" or "misinformation", type:


Since this is a search for a string and not a word, this search pattern may also find such constructions as "misinformationalist" and "disinformationism".

To search for alternative characters in a string, enclose the alternatives in brackets. The search pattern /[md]string will find strings beginning with either m or d. On the other hand, /[d-m]string will find strings beginning with any letter from d through m.

To match zero or more occurrences of the last character, type an asterisk (*) in the string. You can effectively combine brackets and the asterisk to look for well-defined alternatives. For example, to find all strings beginning with a through z and ending with "isinformation" and to find all occurrences of the string "isinformation", type:


6.7.3 Replacing a Character String

The procedure for replacing a text string is based on the search procedures discussed above. All the special matching characters for searches can be used in search-and-replace.

The basic command form is:


followed by the Return key.

Therefore, to replace every occurrence of the string "disinformation" with "newspeak", you would type:


and press Return.

You can modify this command to halt the search and make vi query whether you want to make the replacement in each instance. The following command uses gc (adding c for "consult") to make vi stop at every occurrence of "disinformation" and ask whether you want to make the substitution. Respond with y for yes or n for no.


Note -

You can cancel a "consulted" search-and-replace by pressing Ctrl-C.

6.7.4 Going to a Specific Line

To go to the last line of an open file, by type G. To return to the first line of the file, by type 1G.

You can go to any other line by typing its number followed by G.

For example, suppose that you quit the file paint while editing line 51. You can access that line by opening the file and typing 51G.