Sometimes, halfway through a document or at the end of a draft, you may recognize inconsistencies in the way that you refer to certain things. Or, in a manual, some product whose name appears throughout your file is suddenly renamed (marketing!). Often enough it happens that you have to go back and change what you've already written, and you need to make the changes in several places.
The way to make these changes is with a powerful change command called global replacement. With one command you can automatically replace a word (or a string of characters) wherever it occurs in the file.
In a global replacement, the ex editor checks each line of a file for a given pattern of characters. On all lines where the pattern is found, ex replaces the pattern with a new string of characters. For right now, we'll treat the search pattern as if it were a simple string; later in the chapter we'll look at the powerful pattern-matching language known as regular expressions.
Global replacement really uses two ex commands:
Since the syntax of global replacement
commands can get fairly complex, let's look at it in stages.
The substitute command has the syntax:
This changes the first occurrence of the pattern old to new on the current line. The / (slash) is the delimiter between the various parts of the command. (The slash is optional when it is the last character on the line.) A substitute command with the syntax:
changes every occurrence of old to new on the
current line, not just the first occurrence.
:s command allows options following the substitution string.
g option in the syntax above stands for global.
g option affects each pattern on a line;
don't confuse it with the
:g command, which affects each line of a
By prefixing the
:s command with an address, you can extend its range
to more than one line.
will change every occurrence of old to new from line 50 to line 100.
The following command will change every occurrence of old to new within the entire file:
You can also use
% instead of
1,$ to specify every line in
Thus the last command could also be given like this:
Global replacement is much faster than finding each instance of a string and replacing it individually. Because the command can be used to make many different kinds of changes, and because it is so powerful, we are first going to illustrate simple replacements and then build to complex, context-sensitive replacements.
It makes sense to be overly careful when using a search and replace command.
It sometimes happens that what you get is not what you expect.
You can undo any search and replacement command by
u, provided that the command was the most recent
edit you made.
But you don't always catch undesired changes until it is too late
to undo them.
Another way to protect your edited file is to save the file with
:w before performing a global replacement.
Then at least you can quit the file without saving your edits and
go back to where you were before the change was made.
You can also read the
previous version of the buffer back in with
It's wise to be cautious and know exactly what is going to be
changed in your file.
If you'd like to see what the search turns up
and confirm each replacement before it is made,
c option (for confirm)
at the end of the substitute command:
It will display the entire line where the string has been located, and the string will be marked by a series of carets (^^^^).
copyists at his school ^^^_
If you want to make the replacement, you must enter
If you don't want to make a change, simply press
this can be used for invitations, signs, and menus. ^^^_
The combination of the vi commands
n (repeat last search) and dot (
(repeat last command) is also an extraordinarily useful and
quick way to page through a file and make repetitive changes that
you may not want to make globally. So, for example, if your editor has
told you that
you're using which when you should be using that, you can spot-check
every occurrence of which, changing only those that
|Search for which.|
|Change to that.|
|Repeat change (if appropriate).|