What are the useful flags for the substitute command?
Answer
:s/pattern/replace/flags
Explanation
The substitute command supports several flags that modify its behavior. Understanding them lets you use :s precisely.
How it works
g— replace all occurrences on the line (not just the first)c— confirm each replacementi— case-insensitive matchI— case-sensitive matchn— count matches without replacinge— suppress errors when pattern is not found
Example
:%s/old/new/g " Replace all occurrences
:%s/old/new/gc " Confirm each replacement
:%s/old/new/gi " Replace all, case-insensitive
:%s/old/new/gce " Confirm, no error if not found
Tips
eis essential in:bufdoand:argdoscriptscprompt options:y/n/a(all)/q(quit)/l(last)&repeats flags from the last substitute- Flags can be combined in any order
- Without
g, only the first match per line is replaced