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

How do I rename a variable across all its case variants (camelCase, snake_case, SCREAMING_CASE) in one command?

Answer

:Subvert/{src}/{tgt}/g

Explanation

vim-abolish provides :Subvert, a smarter substitute that detects and preserves the case style of each match. When renaming a concept across a codebase, the same word often appears as foo_bar in variable names, fooBar in method names, FooBar in class names, and FOO_BAR in constants. :Subvert handles all of them in a single command.

How it works

  • :Subvert/{src}/{tgt}/g works like :s but infers the case style of each occurrence and applies the same style to the replacement.
  • It recognizes: snake_case, camelCase, MixedCase, UPPER_CASE, and space-separated words.
  • The source and target are written in any one canonical form — vim-abolish derives the rest.

Example

Running:

:%Subvert/blog_post/article/g

Transforms:

blog_post   → article
blogPost    → article
BlogPost    → Article
BLOG_POST   → ARTICLE

All in one pass, with no need to write four separate :s commands.

Tips

  • You can specify a visual range or line range just like with :s: :'<,'>Subvert/old/new/g.
  • :Subvert also accepts the /c flag for interactive confirmation.
  • The plugin's :Abolish command lets you register abbreviation variants for insert-mode auto-expansion.
  • Install with any plugin manager: tpope/vim-abolish.

Next

How do I open any split command's result in a new tab instead of a split window?