Quick Fix: Code
→ Reformat Code...
(default <kbd>Ctrl</kbd>
+ <kbd>Alt</kbd>
+ <kbd>L</kbd>
) for the whole file or Code
→ Auto-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:
- Go to File → Settings → Editor → Code Style.
- Adjust the desired settings.
Solution 3: Indenting Comments
To indent comments to the same level as code:
- Navigate to Settings → Editor → Code Style → JavaScript → Comments.
- 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:
- Select the code you want to indent.
- Windows: Press Ctrl + Alt + L
- Linux: Press Ctrl + Windows Key + Alt + L
- Mac: Press Command + Option + L
Solution 3: IntelliJ Editor Settings
To automatically correct indentation in IntelliJ:
-
Go to Settings (<kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>S</kbd>).
-
Select Editor > Code Style > Java.
-
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.
-
Click Apply and OK.
-
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:
- Highlight the code that needs indenting.
- Press Ctrl + Alt + L.
- 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.