How to correct indentation in IntelliJ – Indentation

Photo of author
Written By M Ibrahim
indentation intellij-idea

Quick Fix: CodeReformat Code... (default <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>L</kbd>) for the whole file or CodeAuto-Indent Lines (default <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>I</kbd>) for the current line or selection.

The Problem:

In IntelliJ, how can I automatically correct indentation without having to do it manually? In Eclipse, you can simply highlight the code that needs indenting, right-click, and choose Source > Correct indentation. Is there a similar way to do this in IntelliJ?

The Solutions:

Solution 1: Keystroke Reformatting

Use the following keystrokes to reformat code:

  • For the entire file: Code → Reformat Code… (default: Ctrl + Alt + L)
  • For the current line or selection: Code → Auto-Indent Lines (default: Ctrl + Alt + I)

Solution 2: Customizing Auto-Formatting Settings

To customize the auto-formatting settings:

  1. Go to File → Settings → Editor → Code Style.
  2. Adjust the desired settings.

Solution 3: Indenting Comments

To indent comments to the same level as code:

  1. Navigate to Settings → Editor → Code Style → JavaScript → Comments.
  2. Select the desired indentation level.

[Screenshot for JavaScript]
(Note: The specific steps and settings may vary depending on the language.)

Solution 1: Using Keyboard Shortcuts

To automatically correct indentation in IntelliJ using keyboard shortcuts, follow these steps:

  1. Select the code you want to indent.
  2. Windows: Press Ctrl + Alt + L
  3. Linux: Press Ctrl + Windows Key + Alt + L
  4. Mac: Press Command + Option + L

Solution 3: IntelliJ Editor Settings

To automatically correct indentation in IntelliJ:

  1. Go to Settings (<kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>S</kbd>).

  2. Select Editor > Code Style > Java.

  3. Under Code Style (right panel), adjust the following values:

    • Tab Size: Number of spaces in a tab.
    • Indent: Number of spaces for indentation.
    • Continuation Indent: Number of spaces for continuing a line.
  4. Click Apply and OK.

  5. Press <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>L</kbd> to automatically format the code.

Solution 4: Reformat code

To correct indentation in IntelliJ, use the following steps:

  1. Highlight the code that needs indenting.
  2. Press Ctrl + Alt + L.
  3. IntelliJ will automatically correct the indentation.

Solution 5: Go to settings to uncheck “comment at first column”

In Android Studio, navigate to File -> Settings -> Editor -> Code Style -> Java. Under Wrapping and Braces, uncheck “Comment at first Column”. This will allow the formatting shortcut to indent comment lines as well.