Intellij IDEA, format all code in a project

JavaIntellij IdeaCode Formatting

Java Problem Overview


I really like IDEA's code formatting, but how do I get it to reformat all the code in a particular project without going through each file? I've found the option to tidy / optimise imports on code before committing it to subversion which is great, but it only seems to apply to files that have otherwise been modified (which is sensible in the majority of cases!)

Is there a way to auto-format all the source files in a project without going through them all by hand?

Java Solutions


Solution 1 - Java

In the tree-structure, right click on the folder you like to reformat, choose "Reformat code" from the menu.

Solution 2 - Java

Choose the "whole project" radio button instead of the individual file when you ask to reformat.

Just be aware of one problem: If you're working on a larger project with other developers who don't conform to your style, and you're using IntelliJ to transform all their code every time you edit it, you might be causing a problem with your version control system. It will long a huge volume of changes when you check in, and few of them will have anything to do with changing functionality. Better to conform to the group style in that case.

Solution 3 - Java

According the iDea: Editor basics documentation:

You can use the shortcut Ctrl+ALT+L (Windows/Linux) or ⌥⌘+L (MAC OS X) and select the Rearrange entries option to reformat the code in the current file or reformat a module or directory (after selecting more than one file).

You can also Right-click a module, file, or directory from the context menu and select Reformat Code and also select the Rearrange entries option.

enter image description here

You can also optimize the imports of your files by selecting the Optimize imports option.

Is some Linux versions the shortcut Ctrl+ALT+L can lock the screen, so you will need to Right-click a file or directory from the context menu and select Reformat Code and also select the Rearrange entries option.

This works for most of the Jetbrains IDES (iDea, PyCharm, WebStorm, RubyMine, and so on.)

Solution 4 - Java

"CTRL + ALT + L" work only windows. "CTRL + ALT + L" is Ubuntu Lock screen shortcut key so its not working for this.

Try ,

Right click on the folder you like to reformat, choose "Reformat code" from the menu or click "Code" button on navigation bar in top, choose "Reformat code"

Thank you

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
QuestionMichael BerryView Question on Stackoverflow
Solution 1 - JavaFriesgaardView Answer on Stackoverflow
Solution 2 - JavaduffymoView Answer on Stackoverflow
Solution 3 - JavavaldeciView Answer on Stackoverflow
Solution 4 - JavaJasintha PeirisView Answer on Stackoverflow