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

How do I convert a variable name between snake_case, camelCase, and other styles in Vim?

Answer

cr

Explanation

The vim-abolish plugin (by tpope) provides a cr (coerce) operator that converts the word under the cursor between naming conventions with a single keystroke pair. It understands snake_case, camelCase, MixedCase, UPPER_CASE, kebab-case, dot.case, and more — making it indispensable when refactoring variable names across different style conventions.

How it works

cr is followed by a letter specifying the target case:

  • crs — snake_case
  • crc — camelCase
  • crm — MixedCase (PascalCase)
  • cru — UPPER_CASE
  • crk — kebab-case
  • cr. — dot.case
  • cr<Space> — space case
  • crt — Title Case

Example

With the cursor on myVariableName:

crs → my_variable_name
cru → MY_VARIABLE_NAME
crk → my-variable-name
crm → MyVariableName
crc → myVariableName  (back to original)

Tips

  • Install with any plugin manager: Plug 'tpope/vim-abolish'
  • cr works on the word under the cursor — no need to visually select first
  • vim-abolish also provides a powerful :Subvert command for case-aware substitution across a file
  • Works well with the dot command (.) if you need to coerce the same pattern on multiple words

Next

How do I visually select a double-quoted string including the quotes themselves?