9

I often run into a situation where I have a file with a lot of mixed case. For example,

CamelCase and camelCase

I'm looking for a Vim regular expression replacement to produce,

AnotherCase and anotherCase

Right now I do this in two steps after visually selecting the lines,

:s/CamelCase/AnotherCase/g
:s/camelCase/anotherCase/g

Is this possible in one replacement step, or should I look into other options like a function in .vimrc?

wsams
  • 193
  • 5

2 Answers2

7

I would suggest using :Subvert via Tim Pope's Abolish.vim

:%S/camelCase/anotherCase/g

Vimcasts episode: Supercharged substitution with :Subvert

Peter Rincker
  • 15,854
  • 1
  • 36
  • 45
  • This plugin made that so easy. I didn't look under the hood, but thought I'd try :%S/CamelCase/AnotherCase/g, however it only replaces CamelCase and left camelCase as is. No big deal, just something to remember. – wsams Mar 04 '16 at 23:15
  • It's likely using smartcase or something to that effect so that any upper case characters are required to be upper case but lower case can be capitalized as necessary. – dash-tom-bang Mar 05 '16 at 00:19
3

You also have the venerable keepcase.

With

:SubstituteCase#\ctoto\(Titi\)tata#\1Tutu#g

you'll obtain:

 totoTitiTata -> titiTutu
 TotoTitiTata -> TitiTutu
 tototititata -> tititutu
 tototitiTata -> titiTutu
 TototitiTata -> TitiTutu 
Luc Hermitte
  • 17,351
  • 1
  • 33
  • 49