See also this fandom vim wiki page.
:s/find/replace/
, only search in current line, and replace the first matched pattern
- Default: only in current line
- In all lines:
:%s/f/r/
- In selected line:
:'<,'>s/f/r/
- In selected characters:
:`<,`>s/f/r/
- Default: only first matched in given range
- match every one:
:s/f/r/g
- Reuse the matched pattern:
:s/f/&/
&
is the text that matches the search pattern.Example:
:s/[a-z]/&1/g
, add a1
after each characte:s/\(a\)/\1/
\(a\)
makes a backreference toa
.\1
is the first backreference,\2
is the second backreference,\0
is the text matched by the entire pattern.
\<
and\>
to match the beginning and the end of a word.