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?

7

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

:%S/camelCase/anotherCase/g

Vimcasts episode: Supercharged substitution with :Subvert

  • 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 4 '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 5 '16 at 0: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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.