This question already has an answer here:

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


Can I do this in one command?

marked as duplicate by Luc Hermitte, Peter Rincker, nobe4, Martin Tournoij May 19 '16 at 18:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 May 19 '16 at 14:40

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.