Global string substitutions
Last revision August 2, 2004
Table of Contents: |
You can use an ex-style substitute command that searches one or more lines for a specified arbitrary sequence of characters (or a regular expression that matches a pattern) and then replaces that sequence, when found, with another specified sequence of characters.
The basic syntax of this type of substitute command is:
-
:[linerange]s/old/new/[g]<CR>
where the brackets delimit optional portions of the command (the brackets themselves would not be typed). This is one of those "colon" (:) or "ex" commands that is displayed on the status line of the screen (bottom line) while you are typing it, and does not go into effect until you press the RETURN key.
In its simplest form, :s/old/new/<CR>, the arbitrary sequence of characters new replaces the first occurrence of the arbitrary sequence of characters (or regular expression) old that is found on the "current" line (the line on which the cursor was located before typing the colon).
Because old is actually interpreted as a regular expression, just like when you are searching the file, you need to be careful of regular expression metacharacters in the old text. You can escape these metacharacters by preceding them with a backslash if you want to treat them as normal characters. Or, you can turn off regular expression matching altogether, which then treats the metacharacters literally (match that specific character, rather than the pattern it represents), using this command:
:set nomagic
To replace every occurrence of old with new on the line, add the optional g (for "global") to the end of the command, like:
:s/old/new/g
Although it is conventional to use slash characters (/) to delimit the old and new strings in the substitute command, you can actually use any character that is not itself part of either old or new . These examples would also work:
:1s+old+new+
:s'old'new'
:s"old with a slash / in it"new"
Here the slash (/) is part of old. The delimiter is the quote mark.
You use an optional line range in this command to make it apply to occurrences of old on more than one line.
The line range consists of a reference to a beginning line separated from a reference to an ending line by a comma (or just a single line reference). The ending line must be after the beginning line in the file (you cannot go backwards).
Forms of line references:
-
An absolute line number in the file, like 2, 26, or 502. Example:
-
:1,10s/old/new/
- A relative line number like +10 or -5. These numbers are always relative
to the line that the cursor was on before the colon was typed. Example:
-
:-2,+3s/old/new/
- A symbolic line number. There are two:
. The dot stands for the line the cursor was on when the colon was typed.
$ The dollar sign stands for the last line in the file.
For example, this command affects all lines from the current line (where the cursor was before typing the command) to the end of file:
:.,$s/old/new/
Finally, there are two ways to make the substitute command apply to all lines in the file:
First, use the optional line range 1,$, for example:
:1,$s/old/new/
This changes the first occurrence of old in every line. To change every occurrence, add the suffix g to the command, for example:
:1,$s/old/new/g
Or, use the "global" command in combination with the substitute command (read the ex reference manual for other uses of the global command). The syntax for this is:
:g/old/s//new/
Here, the g command searches for all lines with the string old, and the following s command replaces old with new. The double slash after the s stands for the old string.