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
  • 30,012
  • 40
  • 109
  • 197

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
  • 41,641
  • 9
  • 70
  • 96
-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,423
  • 2
  • 37
  • 53