1

Let's say I have:

foo and Foo

How can I find and replace it to

bar and Bar

With one regex/find and replace command in Vim? By default I have my Vim setup to find either uppercase and lowercase when searching. Now I just want to replace keeping the capitalization. Is this even possible?

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201

2 Answers2

8

I recommend Tim Pope's Abolish plugin. It provides the :Subvert or :S command for short that does exactly what you ask.

:%S/foo/bar/g

Drew Neil over at Vimcast has a nice screencast, Supercharged substitution with :Subvert.

Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
-1

I would suggest that you do set ignorecase! before and after your search. If you do case sensitive searching often then you can map this to something. If you do a lot with mixed cases and case matching you might want to look at plugins.

underrun
  • 6,713
  • 2
  • 41
  • 53