-->
Previous Table of Contents Next


Searching and Replacing Text

Text searches in vi can be performed in either direction: forward or backward. Searches are always started from the current cursor location and continue from the top or bottom of the file, depending on which direction you use. In other words, searches “word wrap” the file.

You can use your file asong to illustrate searches. To search forward through asong for the word “bay,” type


/bay

and press Return. Notice that this is a status-line command. The command /bay is echoed on the status line and the cursor is moved to the first occurrence it finds in the forward direction of the string “bay.” Interested in finding another instance of “bay”? Enter a / character. This command continues the search for “bay” in the forward direction and places the cursor at the next instance of “bay.” Each time you enter the / key, vi tries to find an instance of the previous string pattern. When it reaches the end of the file, vi loops back and continues its search at the start of the file.

You can also search backward for strings in vi by using the ? command. It works in exactly the same manner as the / command, but in the opposite direction. Try it out by entering


?I

in asong, instructing vi to search back for instances of “I.” This search can be repeated by typing ?, as you might have suspected. You can continue a search by pressing n, which always continues a search in the same direction as the previous search. However, typing N uses the same search string but in the opposite direction.

As mentioned earlier, searches can be made very powerful through the use of regular expressions. The search command is supplied in the same fashion as described before (/ or ?), but square brackets are added to instruct vi to do a regular expression expansion of the enclosed characters. For example, search forward through asong from the first line for all strings containing the substring “er.” Type


/[*]er[*]

vi’s first matching string arrives at “Where.” If you type n, vi moves the cursor to “watermelon,” and so on. You can also specify collections of characters or ranges of characters to match. Try typing the following:


/[a-z]y

This command used in asong finds the strings “by” and “my,” as well as any word with these strings inside them (such as “bay”). This works because the range of characters given are treated as an enumerated range of ASCII values. Thus, you could also include a range of numbers (for example, 0-9). Now try the following command:


/[Mm]y

This locates the strings “My” and “my.”

In vi, searches without regular expressions find only exact matches of the supplied pattern (including the case of the letters in the pattern). Clearly, regular expressions can be used to enhance many types of searches in which you may not know exactly how a pattern appears in a file.

One of the more common applications of a search is to replace instances of one word (or pattern) with another. This is done with an ex command that starts with a colon. To search the entire asong file for the string “Down” and replace it with the string “Up,” type


:%s/Down/Up/g

The s indicates that this is a search operation, the % means that the entire file is to be searched, “Down” is the pattern to be found, “Up” is the new pattern, and the g tells vi that the search should continue until there are no more pattern matches. Without the g, vi would perform the replacement on only the first match it finds. This command also works with regular expressions appearing in the search pattern and the replacement pattern.

Setting Preferences

vi is configurable, which means that you can set options to control your editing environment. These options are initialized with default values that you can modify in vi at any time. vi is configured using the set command. The set command must be preceded by a colon and entered by pressing Return. For example, to display line numbers in the editor, enter


:set number

The following table describes a few of the more common set commands.

all Displays a list of all available set options and their current status
errorbells Sounds the terminal bell when an error occurs
ignorecase Searches are case-insensitive
number Displays line numbers in the leftmost column of the screen (these are not written to the file)
showmode An indicator appears at the bottom right of the screen if you are in input mode, change mode, replace mode, and so on

set commands that do not take a value can be switched off by inserting a “no” as a prefix to the set parameter. For example, the command


:set nonumber

switches line numbering off. The command


:set

shows only the options that you have changed.

The settings that you use in a vi session are (unfortunately) lost each time you exit vi. If you do not like the idea of resetting these options each time you use vi, there is an easier way to perform this initialization. Use the vi initialization file called .exrc. vi searches for this file in your home directory each time it is invoked. If it can’t find this file, it uses the defaults set within the vi program. As you will see in the following example, the .exrc file can also be used to define vi macros.

A sample .exrc file looks something like this:


set number

set errorbells

set showmode

Note that the colon is not required before a set command in an .exrc file.


Previous Table of Contents Next