Skip to content

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:

TriggerEffect
ySubstitute this match
nSkip this match
qQuit 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.

91. Reuse the Last Search Pattern

92. Replace with the Contents of a Register

93. Repeat the Previous Substitute Command

94. Rearrange CSV Fields Using Submatches