20

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?

1
  • 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
    Commented Dec 2, 2015 at 15:28

2 Answers 2

18

Michaels Geddes' plugin keepcase (GitHub) has all you need:

:%SubstituteCase/\cterm/word/g

Other syntax elements from :substitute are also supported.

1
  • 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
    Commented Dec 2, 2015 at 11:03
22

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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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