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}/gworks like:sbut 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-abolishderives 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. :Subvertalso accepts the/cflag for interactive confirmation.- The plugin's
:Abolishcommand lets you register abbreviation variants for insert-mode auto-expansion. - Install with any plugin manager:
tpope/vim-abolish.