How to correct indentation in IntelliJ

Intellij IdeaIndentation

Intellij Idea Problem Overview


How can indentation be automatically (not manually) corrected in IntelliJ?

In Eclipse, it's possible to just highlight the code that needs indenting, right-click, and select Source > Correct indentation.

Is there any method for doing the same thing in IntelliJ?

Intellij Idea Solutions


Solution 1 - Intellij Idea

CodeReformat Code... (default Ctrl + Alt + L) for the whole file or CodeAuto-Indent Lines (default Ctrl + Alt + I) for the current line or selection.

You can customise the settings for how code is auto-formatted under FileSettingsEditorCode Style.


To ensure comments are also indented to the same level as the code, you can simply do as follows:

UI screenshot (example for JavaScript)

Solution 2 - Intellij Idea

Just select the code and

  • on Windows do Ctrl + Alt + L

  • on Linux do Ctrl + Windows Key + Alt + L

  • on Mac do CMD + Option + L

Solution 3 - Intellij Idea

Select Java editor settings for Intellij [![Settings][1]][1] [1]: https://i.stack.imgur.com/BkRtD.png

Select values for Tabsize, Indent & Continuation Intent (I choose 4,4 & 4)

Then Ctrl + Alt + L to format your file (or your selection).

Solution 4 - Intellij Idea

Ctrl + Alt + L works with Android Studio under xfce4 on Linux. I see that Gnome used to use this shortcut for lock screen, but in Gnome 3 it was changed to Super+L (AKA Windows+L): https://wiki.gnome.org/Design/OS/KeyboardShortcuts

Solution 5 - Intellij Idea

In Android Studio this works: Go to File->Settings->Editor->CodeStyle->Java. Under Wrapping and Braces uncheck "Comment at first Column" Then formatting shortcut will indent the comment lines as well.

Solution 6 - Intellij Idea

You can also try out Ctrl + Alt + I even though you can also use L as well.

Solution 7 - Intellij Idea

Solution of unchecking comment at first column is partially working, because it works for line comments, but not block comments.

So, with lines like:

/* first line
 * second line
 * ...
 */

or

// line 1
// line 2
// line 3
...

they are indented with "Auto reformat", but lines like:

/* first line
   second line
   ...
 */

the identation will not be fixed.

So you should:

  • add * or // before each line of comments
  • then uncheck Keep when reformatting -> comment at first column
  • and Auto reformat.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjunaidpView Question on Stackoverflow
Solution 1 - Intellij IdeaCrazyCoderView Answer on Stackoverflow
Solution 2 - Intellij IdeajunaidpView Answer on Stackoverflow
Solution 3 - Intellij IdeaGapmeister66View Answer on Stackoverflow
Solution 4 - Intellij IdeaJim Van ZandtView Answer on Stackoverflow
Solution 5 - Intellij IdeaL. SpiekermannView Answer on Stackoverflow
Solution 6 - Intellij Ideamsingathi majolaView Answer on Stackoverflow
Solution 7 - Intellij IdeaWesternGunView Answer on Stackoverflow