18

Due to a quirk in the domain-specific language I am working with, I frequently face the task of (selectively, not globally) replacing term or TERM with word or WORD, respectively.

This means, I search case-insensitively for term, and want to replace that with word while keeping the uppercase / lowercase of the original term intact.

Since checking the whole of term for consistent upper-/lowercase would be difficult and is not really necessary, I would settle for uppercase / lowercase of word / WORD being decided on the first letter of term.

How could I achieve this?

  • 2
    Note about the "selectively" for those who don't know it: Just add c to the end of the substitution statements below, and Vim will ask you about every single match. – DevSolar Dec 2 '15 at 15:28
16

Michaels Geddes' plugin keepcase has all you need:

:%SubstituteCase/\cterm/word/g

Other syntax elements from :substitute are also supported.

  • 5
    Brilliant... this will significantly speed up things for me. I also notice that I no longer ask "can this be done", but only "how to do it". Seems I've finally "arrived" at Vim. :-D – DevSolar Dec 2 '15 at 11:03
16

This can be handled by the :Subvert from the abolish plugin:

One time I had an application with a domain model called "facility" that needed to be renamed to "building". So, a simple search and replace, right?

:%s/facility/building/g

Oh, but the case variants!

:%s/Facility/Building/g
:%s/FACILITY/BUILDING/g

Wait, the plural is more than "s" so we need to get that too!

:%s/facilities/buildings/g
:%s/Facilities/Buildings/g
:%s/FACILITIES/BUILDINGS/g

Abolish.vim has your back. One command to do all six, and you can repeat it with & too!

:%Subvert/facilit{y,ies}/building{,s}/g

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.