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

How do I do a case-aware search and replace that handles camelCase, snake_case, and MixedCase variants at once?

Answer

:Subvert/old/new/g

Explanation

vim-abolish (by Tim Pope) extends Vim's substitution with :Subvert, which performs a case-aware, multi-variant replacement in a single command. When you rename foo_bar to baz_qux, :Subvert automatically handles FooBar, FOO_BAR, fooBar, foo-bar, and every other case pattern simultaneously — no regex gymnastics required.

How it works

  • :Subvert/foo_bar/baz_qux/g — replaces all variants:
    • foo_barbaz_qux
    • FooBarBazQux
    • FOO_BARBAZ_QUX
    • foo-barbaz-qux
    • fooBarbazQux

Install with: Plug 'tpope/vim-abolish'

Basic syntax

:%Subvert/old/new/g

Works like :%s/old/new/g but is case-aware.

Multi-word variant

Replace parse_url with decode_uri across a codebase:

:%Subvert/parse_url/decode_uri/g

All case forms are handled. Confirm each one with the c flag:

:%Subvert/parse_url/decode_uri/gc

Abbreviation expansions

vim-abolish also provides :Abolish for typo correction:

:Abolish seperately separately
:Abolish {despa,sepe}rat{e,ed,es,ing,ely,ion,ions,or} {despe,sepa}rat{}

These auto-correct the typos as you type.

Tips

  • :Subvert is the rename tool every programmer wishes Vim had built in
  • The search form works too: /\v%#=1Subvert — use cr{letter} from vim-abolish's coercion feature (crs = snake_case, crc = camelCase, cru = UPPER_CASE)
  • cr{s,c,m,u,k,d,t} coercions work on the word under the cursor to instantly change its case style
  • :help abolish lists all coercion keys

Next

How do I run a search and replace only within a visually selected region?