19

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?

DevSolar
  • 789
  • 1
  • 7
  • 15
  • 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 02 '15 at 15:28

2 Answers2

21

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
mMontu
  • 6,630
  • 21
  • 31
18

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

:%SubstituteCase/\cterm/word/g

Other syntax elements from :substitute are also supported.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
Luc Hermitte
  • 17,351
  • 1
  • 33
  • 49
  • 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 02 '15 at 11:03