Solaris Advanced User's Guide

Replacing a Character String

The procedure for replacing a text string is based on the search procedures that are discussed previously. You can use all the special matching characters for searches in search-and-replace.

The basic command form is:


:g/search-string/s//replace-string/g

Then press the Return key.

Therefore, to replace every occurrence of the string “disinformation” with “newspeak,” type:


:g/disinformation/s//newspeak/g

Then 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.


:g/disinformation/s//newspeak/gc

Note –

You can cancel a “consulted” search-and-replace function by pressing Ctrl-C.