I would like to replace foo with bar and Foo with Bar. I can do


Can I do this in one command?

  • 3
    I don't think it's possible but you can have a look at this plugin: github.com/tpope/vim-abolish it achieve exactly that, and more.
    – nobe4
    Commented May 19, 2016 at 14:40

1 Answer 1


You could try this:

:s/\cfoo/\=submatch(0)[0] ==# 'F' ? 'Bar' : 'bar'/g

The replacement part of the substitution command is an expression which tests whether the first character of the pattern is F. If it is, the expression returns Bar, otherwise bar.

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