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

How do I quickly convert between snake_case, camelCase, and other naming conventions in Vim?

Answer

crs

Explanation

The vim-abolish plugin by Tim Pope provides instant case coercion commands that convert the word under your cursor between common naming conventions. Instead of manually rewriting variable names when switching between languages or coding styles, a single cr command followed by a letter transforms the word in place.

How it works

Place your cursor on any word and press cr followed by the target case format:

crs    " snake_case
crc    " camelCase
crm    " MixedCase (PascalCase)
cru    " UPPER_CASE (SCREAMING_SNAKE)
cr-    " dash-case (kebab-case)
cr.    " dot.case
cr<space> " space case
crt    " Title Case

Example

Starting with the variable myVariableName under the cursor:

crs → my_variable_name
crc → myVariableName
crm → MyVariableName
cru → MY_VARIABLE_NAME
cr- → my-variable-name
cr. → my.variable.name

Abolish intelligently detects word boundaries regardless of the current format. Converting from my_variable_name with crm correctly produces MyVariableName — it understands underscores, capital letters, hyphens, and dots as word separators.

Real-world use cases

  • Porting code between Python and JavaScript: Python uses snake_case, JavaScript uses camelCase. Place cursor on a variable, press crc or crs to convert instantly.
  • Writing CSS class names: Convert a JavaScript camelCase prop to kebab-case for a CSS class with cr-.
  • Constants: Promote a regular variable to a constant with cru to get SCREAMING_SNAKE_CASE.
  • Database columns to model attributes: Many ORMs map snake_case database columns to camelCase or PascalCase model properties.

Tips

  • Case coercion is repeatable with . if you have vim-repeat installed — apply the same conversion to multiple variables rapidly
  • Combine with search: use * to find all occurrences of a variable, then crs + n.n.n. to convert them one by one with review
  • Abolish also provides the :Subvert command for case-aware search and replace across an entire file, which is even more powerful for bulk renaming

Next

How do I edit multiple lines at once using multiple cursors in Vim?