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:

  • crs — snake_case: myVariable → my_variable
  • crm — MixedCase (PascalCase): my_variable → MyVariable
  • crc — camelCase: my_variable → myVariable
  • cru — UPPER_CASE: my_variable → MY_VARIABLE
  • cr- — dash-case (kebab-case): my_variable → my-variable
  • cr. — dot.case: my_variable → my.variable
  • crt — Title Case: my_variable → My 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?