How do I rename a variable across all case variants (snake_case, camelCase, MixedCase) at once?
Answer
:Subvert
Explanation
The vim-abolish plugin's :Subvert command (abbreviated :S) substitutes a word across all its case variants simultaneously. Instead of running three separate substitutions for snake_case, camelCase, and MixedCase forms of the same word, a single :Subvert handles all of them and preserves the case pattern in the replacement.
How it works
:%S/old_word/new_word/g
Abolish detects all inflections of old_word in the file and replaces each one with the corresponding inflection of new_word. The case pattern of each match is preserved in the replacement.
Supported variants automatically handled:
old_word→new_word(snake_case)OldWord→NewWord(MixedCase / PascalCase)oldWord→newWord(camelCase)OLD_WORD→NEW_WORD(SCREAMING_SNAKE)old-word→new-word(kebab-case)
Example
Renaming user_name to display_name across a codebase:
:%S/user_name/display_name/g
This automatically renames:
Before: After:
userName → displayName
UserName → DisplayName
USER_NAME → DISPLAY_NAME
user_name → display_name
Tips
- Install with your plugin manager:
Plug 'tpope/vim-abolish' - Use
:%S/{old,Old}/{new,New}/gfor manual variant specification when the pattern doesn't follow standard naming conventions - Abolish also provides
crcoercion commands to change a word's case style under the cursor:crs(snake_case),crm(MixedCase),crc(camelCase),cru(UPPER_CASE) - The lowercase
:s(built-in substitute) still works normally —:Sis the abolish-specific command