:s/foo/bar Replace the first instance of foo with bar on the current line.
:s/foo/bar/g Replace every instance of foo with bar on the current line.
:%s/foo/bar/g Replace foo with bar throughout the entire file.
* on the word you want to substitute.
:%s//replacement/g, leaving the find pattern empty.
:s/foo/bar/c Marks the first instance of foo on the line and asks for confirmation for substitution with bar
:%s/foo/bar/gc Marks consecutively every match of foo in the file and asks for confirmation for substitution with bar
For example, with following nmap:
nmap <expr> <S-F6> ':%s/' . @/ . '//gc<LEFT><LEFT><LEFT>'
select a word with *, type Shift-F6, type in a replacement and hit Enter to rename all occurrences interactively.
s/<pattern>/<pattern>/optional-flags<pattern> is a Regex| Flag | Meaning |
|---|---|
| & | Keep the flags from the previous substitute. |
| c | Prompt to confirm each substitution. |
| e | Do not report errors. |
| g | Replace all occurrences in the line. |
| i | Case-insensitive matching. |
| I | Case-sensitive matching. |
| n | Report the number of matches, do not actually substitute. |