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_casecrc—camelCasecrm—MixedCase(PascalCase)cru—UPPER_CASEcrk—kebab-casecr.—dot.casecr<Space>—space casecrt—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' crworks on the word under the cursor — no need to visually select first- vim-abolish also provides a powerful
:Subvertcommand for case-aware substitution across a file - Works well with the dot command (
.) if you need to coerce the same pattern on multiple words