14.Substitution
88. Meet the Substitute Command
89. Find and Replace Every Match in a File
90. Eyeball Each Substitution
Finding all occurrences of a pattern and blindly replacing them with something else won’t always work. Sometimes we need to look at each match and decide if it should be substituted. The c
flag modifies the :substitute
command to make this possible.
:%s/pattern/target/gc
: c
means "confirm each substitution"
We aren’t limited to just two answers. In fact, Vim helpfully reminds us of our options with the prompt “y/n/a/q/l/^E/^Y.” This table shows what each answer means:
Trigger | Effect |
---|---|
y | Substitute this match |
n | Skip this match |
q | Quit substituting |
l | “last”—Substitute this match, then quit |
a | “all”—Substitute this and any remaining matches |
<C-e> | Scroll the screen up |
<C-y> | Scroll the screen down |
You can also find this information in Vim’s help by looking up :h :s_c
.