3

For example, for a line

X X X

with the command

:s/X/Y/c

vim will only replace the first occurrence, namely the first 'X' to 'Y', is there a way to let vim go through all the 'X' and replace it while asking for confirmation for each replacement?

2
  • Welcome to Vi and Vim!
    – filbranden
    Dec 17, 2021 at 15:06
  • 1
    thx, I just referred to vimregex.com and only interpret the meaning of 'c' literally as 'Confirm Each substitution', now I know what does 'Each' really mean :) Dec 17, 2021 at 22:31

2 Answers 2

11

By default Vim replaces only first occurrence. You should add g to you substitution: :s/X/Y/cg.

Vim will then let you change each X one by one (confirming with y) or all at once (confirming with a).

0

I cannot add comment to previous answer, so I decided to post my own.

As it's already been mentioned, you should use 'g' flag to replace all occurrences. But for :s/X/Y/cg it will still replace only in current line. Reference from :help :s

4.2 Substitute                      *:substitute*
                            *:s* *:su*
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

            ......
    
            When [range] and [count] are omitted, replace in the
            current line only.

            ......

You can specify range after colon symbol, like % (:%s/X/Y/cg), to replace in the whole file. Or you can specify range from current line to document's end by .,$ (:.,$s/X/Y/cg). More about ranges you can see in :help :range:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.