Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA: How to set auto format on file save JAVA [duplicate]

I know that IntelliJ perform code formatting using the short Key ctrl+alt+L. Is there anyway we can set it up automatically on file save as it happen in eclipse.

like image 514
Ashish Avatar asked Jan 03 '14 21:01

Ashish


1 Answers

There is not, at least not directly. The reason being is that IDEA routinely saves files (after a period of inactivity, before a unit test is run, before make, etc.) It's a bit of a different philosophy. Because of this, doing a reformat on a save would cause a lot of "code jumping". Secondly, Since IDEA formats the code as you type, and can be set to format when pasting code, code should (mostly) already be properly formatted. Of course, the some of the more subtle things (number of blank lines between methods for example) need a code reformat. Note that you can set it so code is formatted before being committed to source control. There's an option for it in the commit dialog.

If you really want this behavior, I suggest the following. Record a macro (Edit > Macros) that does a reformat followed by a save. Save the Macro with a name (i.e. a non-temporary macro). Next, go into the keymap in Settings (File > Settings > [IDE Settings] Keymap) and remove the Ctrl+S mapping from Save and map it to your macro.

EDIT

I didn't initially notice vlcekmi3's duplicate comment. One answer to that question has a similar IDEA of using a macro.

like image 69
Javaru Avatar answered Oct 11 '22 20:10

Javaru