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

How do I convert between snake_case, camelCase, MixedCase, and UPPER_CASE using vim-abolish coercions?

Answer

crs / crm / crc / cru

Explanation

The vim-abolish plugin by Tim Pope adds a cr (coerce) prefix command that instantly converts the word under the cursor between common naming conventions. This is invaluable when renaming variables, translating code between styles, or refactoring identifiers across a codebase.

How it works

Place the cursor on any word and use one of these coercions:

  • crssnake_case: myVariablemy_variable
  • crmMixedCase (PascalCase): my_variableMyVariable
  • crccamelCase: my_variablemyVariable
  • cruUPPER_CASE: my_variableMY_VARIABLE
  • cr-dash-case (kebab-case): my_variablemy-variable
  • cr.dot.case: my_variablemy.variable
  • crtTitle Case: my_variableMy Variable

Each coercion works on the word under the cursor in normal mode and respects word boundaries intelligently.

Example

With cursor on userProfileData:

const userProfileData = {};

Pressing crs converts it to:

const user_profile_data = {};

Then cru would convert it to:

const USER_PROFILE_DATA = {};

Tips

  • Combine with :s and gn for project-wide renaming: search for user_profile_data, then crc. on each match to convert to camelCase
  • Works on acronyms too — URLParser becomes url_parser with crs
  • vim-abolish also provides :Subvert for case-aware substitution across naming conventions in a single command
  • Install via any plugin manager: tpope/vim-abolish

Next

How do I enable matchit so % jumps between if/else/end style pairs?