Intellij IDEA, format all code in a project
Asked Answered
K

4

165

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?

Killerdiller answered 12/3, 2011 at 16:32 Comment(0)
L
286

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

Lacerated answered 12/3, 2011 at 16:36 Comment(4)
What if I want to format code for all of my 50+ modules inside my project? Is there a way to format code for multiple modules at once?Phratry
@Phratry - Much has changed the last 10 years. I would probably use a commandline tools to change files. E.g. we use gradle to format our android/kotlin kode with gw lintFix command.Lacerated
Okay. As this question is specific about the tool to use the command line tool won't help in this case. Anyway I found a way using the file breadcrumb to reformat code in IntelliJ starting in an arbitrary folder and traversing through all subdirectories: gifyu.com/image/JufU Given that all 50+ modules reside in the same parent directory you can reformat all modules at once.Phratry
@Phratry Nice solution for multi project !Lacerated
S
21

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.

Summersault answered 12/3, 2011 at 16:36 Comment(1)
Thanks for the answer, exactly what I was looking for! In this case that isn't an issue; it's a smaller project and we've all agreed on the conventions for the whole thing. For larger projects or in different situations though I totally agree.Killerdiller
R
15

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.)

Reimers answered 30/8, 2018 at 19:41 Comment(1)
u made my day - for Mac ⌥⌘+L (MAC OS X)Tooth
F
2

"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

Fanaticism answered 12/3, 2011 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.