How do I search and replace with case-preserving substitution?
Answer
:%S/old/new/g (vim-abolish)
Explanation
vim-abolish by Tim Pope provides :%S (Subvert), a substitute command that preserves the case pattern of the original text.
How it works
:%S/facility/building/greplaces all case variants:facility->buildingFacility->BuildingFACILITY->BUILDING
- One command handles all case combinations
Example
:%S/error/warning/g
Replaces error with warning, Error with Warning, ERROR with WARNING.
Tips
- Install:
Plug 'tpope/vim-abolish' - Also provides
crfor coercion:crs(snake_case),crc(camelCase) crmconverts to MixedCase,cruto UPPER_CASE:%S/{old,another}/{new,other}/ghandles multiple patterns- Works with Vim's regex features