vimtricks.wiki Concise Vim tricks, one at a time.

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 replacement
  • i — case-insensitive match
  • I — case-sensitive match
  • n — count matches without replacing
  • e — 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

  • e is essential in :bufdo and :argdo scripts
  • c prompt 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

Next

How do you yank a single word into a named register?