In Intellij, how do I toggle between camel case and underscore spaced?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In IntelliJ IDEA, developers often need to switch between different naming conventions such as camel case and underscore spaced (also known as snake case). This is particularly useful when coding in languages like Java or Python, which often require distinct naming styles for variables or methods.
Introduction
Modern integrated development environments (IDEs) like IntelliJ IDEA offer various features to enhance productivity, including tools for refactoring and naming conventions. One such feature is the capability to toggle between camel case and underscore spaced. This transformation can save significant time and effort, especially in large codebases.
Understanding Naming Conventions
- Camel Case: Used primarily in Java, this notation starts with a lowercase letter and capitalizes the first letter of subsequent concatenated words (e.g.,
exampleVariableName). - Underscore Spaced (Snake Case): Commonly used in Python, each word is separated by an underscore and generally written in lowercase (e.g.,
example_variable_name).
How to Toggle in IntelliJ
IntelliJ IDEA does not natively provide a direct shortcut or feature to toggle between camel case and underscore spaced. However, you can achieve this using several different approaches:
Using Live Templates
- Create a Live Template:
- Navigate to
File > Settings > Editor > Live Templates. - Click on
+to create a new template and define an abbreviation such ascc2us(camel case to underscore spaced). - Enter a Groovy script to transform the text between the two conventions.
- Groovy Script Example:
- For camel case to underscore:
- For underscore to camel case:
- Now you can use these templates by typing
cc2usorus2ccfollowed byTabon the selected text.
Using Third-Party Plugins
- String Manipulation Plugin:
- Go to
File > Settings > Pluginsand search for "String Manipulation". - Install the plugin and restart IntelliJ IDEA.
- Select the text you want to transform.
- Right-click and choose
String Manipulation > Toggle Styleto switch between the styles.
Using Custom Code
If you are comfortable with Java or Kotlin, you can develop and use an IntelliJ plugin to automate this process further. This involves understanding IntelliJ's plugin architecture and potentially writing action classes to handle transformations.
Use Cases and Benefits
- Readability: Depending on the language and team conventions, toggling between these styles can make code more readable and maintainable.
- Consistency: Automated transformations help maintain consistency across a codebase.
- Productivity: Reduces manual effort and eliminates the need for a developer to individually rename identifiers across a project.
Key Points Summary
| Feature | Description |
| Camel Case | exampleVariableName: concatenated with upper-case initials. |
| Underscore Spaced | example_variable_name: lowercase words separated by _. |
| Transformation Methods | Live Templates, Third-party Plugins, Custom Code. |
| Tools | Groovy in Live Templates, String Manipulation Plugin. |
| Use Cases | Readability, consistency in naming, improved productivity. |
Conclusion
Although IntelliJ IDEA does not provide an out-of-the-box toggle for camel case and underscore spaced transformations, using live templates, third-party plugins, or custom plugins can effectively accomplish this task. These solutions can significantly improve code consistency and efficiency, allowing developers to focus more on logic and architecture than on code formatting.

