->Title Page
->Vi editor
--->Moving around
--->Deleting text
--->Saving and quitting
->Search and Replace
-->Vi reference
-->Miscellaneous tips
->Mirror sites
->Further reading

[ Up ]

Vi ---> Search and Replace

The search command is /. To search for polite type /polite. n repeats the search in the same direction, and N repeats the search in the opposite direction.

The search option accepts most of the standard Unix pattern matching language. (See the Wildcard section.) Suppose I had a file that contained the following text:

There was a young man of Milan
Whose poems, they never would scan;
When asked why it was,
He said, `It's because
I always try to get as many words into the last line as I possibly can'.

Here are a few examples (using this text) that you will probably never use but may find inspiring:

will search for any lowercase letter followed by as. In this example, it would find was and last but not as or asked.

will search for any an preceded by any character other than a c. In our text it would find Milan but not scan or can.

    /^[A-Z].*\. *$
will search for any line that begins with a capital letter and ends with a period and any number of blanks. Our only match in the example text would be with the last line.

All of these search patterns can be used in the search and replace command that takes on the following structure:

This command replaces every search_string on the current line with replacement_string. Omitting the g (global) flag at the end of the command will cause only the first occurrence of search_string to be altered. Often you may wish to confirm each replacement. This can be done with the confirm flag c. The confirm flag should be placed after or in place of the g flag. Suppose I had the following line:

Give a skeptic and inch... and he'll take a mile.

and typed

    :s/take a mile/measure it/
I would be left with

Give a skeptic and inch... and he'll measure it.

Any command that begins with a ":" is called a line mode command and performs its duty on the line the cursor is currently on. However, you can override vi's default of operating only on the current line by preceding them with a range of line numbers. For example, if I wanted to replace guy with gal on lines 32 through 56 I would type

Omitting the g would cause only the first occurrence of guy in each line to be replaced. The "." and "$" play a special role in this sort of designation. "." indicates the current line, and "$" indicates the last line of the file. Therefore, if I wanted to delete1 from the current line to the end of the file I would enter:2

I could even do something like:

which would delete from the current line to the next line that contained Edison.

One other shortcut that might be worth mentioning is that 1,$ and % both indicate all the lines in the file. Therefore,


do exactly the same thing.

Site Statistics

1 This works because :d is a line mode command that deletes the current line.
2 The same could be accomplished by typing dG.

t a y l o r@l i n u x b o x.c o m

© 1993-1998 Christopher C. Taylor