2

I tried to replace if(cond1) with if(cond1&&cond2) in vim, so I typed the command

:%s/\Vif(cond1)/if(cond1&&cond2)/g

but the result is
if(cond1if(cond1)if(cond1)cond2) and not
if(cond1&&cond2)

What command should I actually use?

3

& in a replacement is a bit special:

magic  nomagic  action
  &      \&     replaced with the whole matched pattern      *s/\&*

But you can escape it with \:

%substitute/\Vif(cond1)/if(cond1\&\&cond2)/g
| improve this answer | |
  • it works, thanks a lot! – HaraInanba Sep 16 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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