I would like to replace foo
with bar
and Foo
with Bar
. I can do
:s/Foo/Bar/g
:s/foo/bar/g
Can I do this in one command?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI would like to replace foo
with bar
and Foo
with Bar
. I can do
:s/Foo/Bar/g
:s/foo/bar/g
Can I do this in one command?
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
.