Global Replacement really uses two ex commands: :g (global) and :s (substitute).

The substitute command has the syntax:

:s/old/new/ - change the first occurrence of the pattern old to new on the current line

:s/old/new/g - change every occurrence on the current line

By prefixing :s command with address you can extend its range to more than one line.

Confirming Substitutions

:w your file before performing a global replacement. If you’d like to see what the search turns up and confirm each replacement before it is made, add the c option(for confirm) at the end of the substitute command:

:1,30s/his/the/gc

Context-Sensitive Placement

:g/pattern/s/old/new/g

The first g tells the command to operate on all lines of a file.

Pattern-Matching Rules

Note that regular expressions can be used with the vi search command / and ?, as well as in the ex commands :g and :s

\( \)

save the pattern enclosed between \( and \) in to a special holding space, or a “holding buffer”. Up to nine patterns can be saved in this way on a single line.

For example:

\(That\) or \(this\)

saves That in bold buffer number 1 and saves this in hold buffer number2. the patterns held can be “replayed” in substitutions by the sequences \1 to \9. To rephrase That or this to read this or That , you could enter:

\(That\) or \(this\)/\2 or \1/

You can alse use the \n notation within a search ori substitute string:

:s/\(abcd\)\1/alphabet-soup/

changes abcd into alphabet-soup

\< \>

Matches characters at the beginning (\<) or at the end (\>) of a word. The end or beginning of a word is determined either by a punctuation mark or by a space. Note that unlike \(..\), these do not have to be used in matched pairs.

~

Matches whatever regular expression was used in the last search. For example, if you searched for The, you could search for Then with /~n. Note that you can use this pattern only in a regular search (with /).

More substitution Tricks
  • A simple :s is the same as :s//~/. In other words. repeat the last substitute command. This can save enormous amounts of time and typing when you are working your way through a document making the same change repeatedly but you don’t want to use a global substitution.
  • If you think of the & as meaning “the same thing”, this command is relatively mnemonic. you can follow the & with a g, to make the substitution globally on the line, and even use it with a ling range:
:%&g - Repeat the last substitution everywhere
  • the & key can be used as a vi command to perform the :g command, to repeat the last substitution. This can save even more typing than :s ENTER – one key-stroke versus three.
  • the :~ command is similar to the :& command but with a subtle difference. The search pattern used is the last regular expression used in any command, not necessarily the one used in the last substitute command.
:s/red/blue/
:/green
:~

the :~ is equivalent to :s/green/blue/.

  • Beside the / character, you may use any nonalphanumeric, nonwhitespace character as your delimiter, except backslash, double quotes, and the vertical bar (\, “, and |). This is particularly handy when you have to make a change to a pathname.
:%s;/user1/tim;/home/tim;g
  • When the edcompatible option is enable, vi remembers the flags (g for global and c for confirmation) used on the last substitution and applies them to the next one.
    This is most useful when you are moving through a file and you wish to make global substitution. You can make the first change:
:s/old/new/g
:set edcompatible

and after that, subsequent substitute commands will be global.