Maintaining a clean and consistent code style is a fundamental requirement for professional software development. IntelliJ IDEA, the industry-leading IDE from JetBrains, provides a powerful engine to reformat code according to predefined rules. Whether you need to fix indents, organize imports, or ensure your entire project follows a team-wide standard like Google Java Style, this guide covers every method available to format code effectively.

The quickest way to format code in IntelliJ IDEA is using the dedicated keyboard shortcut.

  • Windows/Linux: Press Ctrl + Alt + L
  • macOS: Press Option + Command + L

While these shortcuts solve immediate needs, IntelliJ IDEA offers much more sophisticated automation and configuration options that can significantly improve your workflow.

Master Keyboard Shortcuts for Code Formatting

Keyboard shortcuts are the lifeblood of productivity in any IDE. In IntelliJ IDEA, the formatting engine distinguishes between partial and full file reformatting based on your current selection.

Reformatting the Entire File

If no text is selected, pressing the reformat shortcut (Ctrl + Alt + L or Opt + Cmd + L) will apply the current code style rules to the entire active file. This is ideal for quickly cleaning up a file after a heavy coding session.

Reformatting a Specific Code Fragment

Often, you might only want to clean up a specific method or a complex conditional block without touching the rest of the file—especially if you are working on a legacy codebase where a full file reformat would create massive, unnecessary Git diffs. To do this, simply highlight the block of code you want to fix and press the shortcut. IntelliJ IDEA will only adjust the selected lines.

The Advanced "Reformat File" Dialog

If you need more control over what happens during the formatting process, you should use the advanced dialog shortcut:

  • Windows/Linux: Ctrl + Alt + Shift + L
  • macOS: Option + Shift + Command + L

This opens a popup with several critical options:

  1. Optimize imports: Removes unused import statements and reorders the remaining ones according to your style settings.
  2. Rearrange code: Reorders methods, fields, and classes based on the arrangement rules (e.g., placing constants above constructors).
  3. Code cleanup: Runs specific inspections and applies quick-fixes automatically.
  4. Only changed lines: If your project is under version control (like Git), this option ensures only the lines you have modified are reformatted, preventing conflicts with your teammates' work.

Configuring Automatic Formatting on Save

Manually pressing shortcuts is effective, but automation is better. Modern versions of IntelliJ IDEA (2020.1 and later) have streamlined the process of reformatting code whenever you save your work.

How to Enable Actions on Save

To set up automatic formatting, follow these steps:

  1. Open the Settings (Windows/Linux: Ctrl + Alt + S) or Preferences (macOS: Command + ,).
  2. Navigate to Tools | Actions on Save.
  3. Check the box labeled Reformat code.
  4. Optionally, you can click on Configure scope to limit this behavior to specific file types (like *.java or *.kt) or specific directories.
  5. It is also highly recommended to check Optimize imports in the same menu to keep your import blocks clean without manual intervention.

By enabling this, every time you press Ctrl + S or the IDE auto-saves, your code will instantly snap into the correct alignment.

Customizing Your Code Style Settings

Every developer or team has different preferences for indentation, brace placement, and line wrapping. IntelliJ IDEA allows you to customize these down to the finest detail.

Accessing Code Style Configuration

Go to Settings/Preferences | Editor | Code Style. Here, you will see a list of supported languages (Java, Kotlin, JavaScript, HTML, etc.).

When you select a language, you can adjust the following tabs:

  • Tabs and Indents: Set the indent size (default is usually 4), choose between tabs and spaces, and configure "Smart Tabs" behavior.
  • Spaces: Define exactly where spaces should be inserted (e.g., before/after parentheses, around operators, or inside list literals).
  • Wrapping and Braces: Configure how the IDE handles long lines. You can set a "Hard wrap at" value (commonly 120 characters) and decide if braces should be on the same line or a new line.
  • Blank Lines: Control how many empty lines are allowed between methods, class headers, and imports.

Using Predefined Style Schemes

If you don't want to configure every setting manually, you can use the Set from... button at the top of the Code Style page. This allows you to apply industry-standard styles, such as the Google Java Style Guide or the JetBrains default style, with a single click.

Team Collaboration with EditorConfig

In a team environment, having different IDE settings among developers can lead to "formatting wars" in pull requests. To solve this, IntelliJ IDEA natively supports .editorconfig files.

What is .editorconfig?

An .editorconfig file is a simple text file that resides in your project root. It defines formatting rules that are recognized by almost all major IDEs and editors. When a .editorconfig file is present, IntelliJ IDEA will prioritize its settings over your local IDE preferences.

Example of a basic .editorconfig for a Java project: