Replace all occurrences in the line. Without this argument,
replacement occurs only for the first occurrence in each line. If the
'gdefault' option is on, this flag is on by default and the [g]
argument switches it off.
*:s_i*
[i] Ignore case for the pattern. The 'ignorecase' and 'smartcase' options
are not used.
*:s_I*
[I] Don't ignore case for the pattern. The 'ignorecase' and 'smartcase'
options are not used.
*:s_n*
[n] Report the number of matches, do not actually substitute. The [c]
flag is ignored. The matches are reported as if 'report' is zero.
Useful to |count-items|.
If \= |sub-replace-expression| is used, the expression will be
evaluated in the |sandbox| at every match.
[p] Print the line containing the last substitute. *:s_p*
[#] Like [p] and prepend the line number. *:s_#*
[l] Like [p] but print the text like |:list|. *:s_l*
*:s_r*
[r] Only useful in combination with `:&` or `:s` without arguments. `:&r`
works the same way as `:~`: When the search pattern is empty, use the
previously used search pattern instead of the search pattern from the
last substitute or `:global`. If the last command that did a search
was a substitute or `:global`, there is no effect. If the last
command was a search command such as "/", use the pattern from that
command.
For `:s` with an argument this already happens: >
:s/blue/red/
/green
:s//red/ or :~ or :&r
< The last commands will replace "green" with "red". >
:s/blue/red/
/green
:&
< The last command will replace "blue" with "red".
Note that there is no flag to change the "magicness" of the pattern. A
different command is used instead, or you can use |/\v| and friends. The
reason is that the flags can only be found by skipping the pattern, and in
order to skip the pattern the "magicness" must be known. Catch 22!
If the {pattern} for the substitute command is empty, the command uses the
pattern from the last substitute or `:global` command. If there is none, but
there is a previous search pattern, that one is used. With the [r] flag, the
command uses the pattern from the last substitute, `:global`, or search
command.
If the {string} is omitted the substitute is done as if it's empty. Thus the
matched pattern is deleted. The separator after {pattern} can also be left
out then. Example: >
:%s/TESTING
This deletes "TESTING" from all lines, but only one per line.
For compatibility with Vi these two exceptions are allowed:
"\/{string}/" and "\?{string}?" do the same as "//{string}/r".
"\&{string}&" does the same as "//{string}/".
*pattern-delimiter* *E146*
Instead of the '/' which surrounds the pattern and replacement string, you can
use another single-byte character. This is useful if you want to include a
'/' in the search pattern or replacement string. Example: >
:s+/+//+
You can use most characters, but not an alphanumeric character, '\', '"' or
'|'.
For the definition of a pattern, see |pattern|. In Visual block mode, use
|/\%V| in the pattern to have the substitute work in the block only.
Otherwise it works on whole lines anyway.
*sub-replace-special* *:s\=*
When the {string} starts with "\=" it is evaluated as an expression, see
|sub-replace-expression|. You can use that for complex replacement or special
characters.
The substitution is limited in recursion to 4 levels. *E1290*
Otherwise these characters in {string} have a special meaning:
magic nomagic action ~
& \& replaced with the whole matched pattern *s/\&*
\& & replaced with &
\0 replaced with the whole matched pattern *\0* *s/\0*
\1 replaced with the matched pattern in the first
pair of () *s/\1*
\2 replaced with the matched pattern in the second
pair of () *s/\2*
.. .. *s/\3*
\9 replaced with the matched pattern in the ninth
pair of () *s/\9*
~ \~ replaced with the {string} of the previous