vimtricks.wiki Concise Vim tricks, one at a time.

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/g replaces all case variants:
    • facility -> building
    • Facility -> Building
    • FACILITY -> 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 cr for coercion: crs (snake_case), crc (camelCase)
  • crm converts to MixedCase, cru to UPPER_CASE
  • :%S/{old,another}/{new,other}/g handles multiple patterns
  • Works with Vim's regex features

Next

How do you yank a single word into a named register?