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

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_wordnew_word (snake_case)
  • OldWordNewWord (MixedCase / PascalCase)
  • oldWordnewWord (camelCase)
  • OLD_WORDNEW_WORD (SCREAMING_SNAKE)
  • old-wordnew-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}/g for manual variant specification when the pattern doesn't follow standard naming conventions
  • Abolish also provides cr coercion 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 — :S is the abolish-specific command

Next

How do I paste back a small deletion (like a deleted character) while in insert mode?