Explain Codes LogoExplain Codes Logo

In Intellij, how do I toggle between camel case and underscore spaced?

java
intellij
regex
naming-conventions
Alex KataevbyAlex Kataev·Dec 2, 2024
TLDR

Quickly flip between camelCase and snake_case in IntelliJ with Shift + Alt + U. You just need to place your cursor on the name and hit the keys to toggle.

Example:

  • myVariablemy_variable.

How do plugins and shortcuts help?

Boost your IntelliJ navigation with the aid of handy plugins like String Manipulation and CamelCase. They come loaded with text transformations, including the much needed naming convention swaps. Do remember to set up keyboard shortcuts that match your workflow to speed up your coding and ensure consistency.

Regex for manual toggling

When plugins are unavailable, IntelliJ's Find and Replace feature is your lifeline with its support for regular expressions (regex). Craft your own patterns to flip naming conventions:

Converting from snake_case to CamelCase can be done with the following pattern and replacement:

  • Pattern: (_)([a-z])
  • Replacement: `\U$2 // The sneaky snake sheds its skin and becomes a camel!``

For flipping from CamelCase to snake_case, employ this structure:

  • Pattern: ([a-z])([A-Z])
  • Replacement: $1_$2 // The towering camel crouches to become a snake!

It's crucial that you set Regex option in Find/Replace and consider enabling Match Case for precise toggling.

Ensuring consistency while toggling

It's essential to uphold standardized naming conventions especially during your toggling spree across varying programming languages. From Java to SQL to Clojure, consistent naming enhances code readability and curbs errors. It's wise to take into account naming conventions like the use of leading underscore for private variables in some languages and their idiomatic translation in other languages.

Customizing for ease of use

A touch of personal customization can significantly augment your productivity. Whether it's tailoring your regex patterns or exploiting the String Manipulation plugin, gearing up for project-specific naming standards is a breeze. Learning to access and modify keyboard shortcuts can rev up the transition between naming styles.

Dual view feature

The virtue of seeing both naming conventions in tandem cannot be overstated. IntelliJ or PyCharm’s refactoring tools allow for a sneak-peek of snake_case versions in a drop-down list. This empowers you to visualize the scope of variables and functions that might need renaming to adapt to different codebase requirements.