I remember seeing in either IntelliJ or Eclipse the setting to reformat (cleanup) files whenever they are saved. How do I find it (didn't find it in the settings)
14 Answers
This solution worked better for me:
- Make a macro (I used Organize Imports, Format Code, Save All)
- Assign it a keystroke (I overrode Ctrl+S)
Note: You will have to check the box "Do not show this message again" the first time for the organized imports, but it works as expected after that.
Step-by-step for IntelliJ 10.0:
- Code -> "Optimize Imports...", if a dialogue box appears, check the box that says "Do not show this message again.", then click "Run".
- Tools -> "Start Macro Recording"
- Code -> "Optimize Imports..."
- Code -> "Reformat Code..."
- File -> "Save all"
- Tools -> "Stop Macro Recording"
- Name the macro (something like "formatted save")
- In File -> Settings -> Keymap, select your macro located at "Main Menu -> Tools -> "formatted save"
- Click "Add Keyboard Shortcut", then perform the keystroke you want. If you choose Ctrl+S like me, it will ask you what to do with the previous Ctrl+S shortcut. Remove it. You can always reassign it later if you want.
- Enjoy!
For IntelliJ 11, replace
step 2. with: Edit -> Macros -> "Start Macro Recording"
step 6. with: Edit -> Macros -> "Stop Macro Recording"
Everything else remains the same.
IntelliJ 12
8. The Preferences contain the Keymap settings. Use the input field to filter the content, as shown in the screenshot.

- 6,572
- 8
- 46
- 58

- 4,719
- 2
- 14
- 2
-
4In IntelliJ 11: 1: Start macro recording: Edit -> Macros -> Start macro recording. 2: Follow instructions above. 3: Stop macro recording: Edit -> Macros -> Stop macro recording. In the Keymap the macro is saved to the Macros folder instead of the Main Menu -> Tools folder. – Markus K Feb 10 '12 at 08:30
-
3If you're happy with how code is reformatted then this approach works much better if you choose to reformat **all** code in the project and save all. Otherwise when you make multi-file edits only the active one is reformatted before all modified are saved. – Drew Noakes Feb 26 '14 at 15:07
-
i am using intellij 11. The macro formates only the file which is open and not the whole project. What can i do? – Sal-laS Oct 01 '20 at 08:34
-
3in V 2021.2.2 this has become a built in feature in `File | Settings | Tools | Actions on Save` – elonderin Oct 08 '21 at 12:49
I suggest the save actions plugin. It also supports optimize imports and rearrange code.
Works well in combination with the eclipse formatter plugin.
Search and activate the plugin:
Configure it:
Edit: it seems like it the recent version of Intellij the save action plugin is triggered by the automatic Intellij save. This can be quite annoying when it hits while still editing.
This github issue of the plugin gives a hint to some possible solutions:
https://github.com/dubreuia/intellij-plugin-save-actions/issues/63
I actually tried to assign reformat to Ctrl+S and it worked fine - saving is done automatically now.

- 4,765
- 8
- 40
- 57

- 4,009
- 2
- 23
- 23
-
I used this but after updating to Android Studio 1.5.1, I can't find it anymore. – r1k0 Mar 30 '16 at 10:47
-
1The Plugin was (as of now) last updated _10.02.2016_ and says `Compatibility This plugin works with Java 6+ and Intellij IDEA Community Edition after 131 (around version 13).` I don't know which Version of IntelliJ Android Studio is based on. Can you check this? – Benjamin Peter Mar 31 '16 at 07:51
-
Almost worked for me. Unfortunately, the "reformat code" would do only a subset of the things that "Code > Reformat code" does (at least for Scala code). Why is that? – Thilo Aug 24 '16 at 00:02
-
Tried this but IntelliJ saves far to often. Organizing imports pops up ever 2-3 keystrokes. – Ashley Nov 18 '16 at 20:12
-
Confirming Ash, in my case it launches reformatting every 2 seconds.. :O OMG – syr Feb 28 '17 at 16:00
-
There seem to be hints on how to solve this issue in this github thread of the plugin https://github.com/dubreuia/intellij-plugin-save-actions/issues/63 – Benjamin Peter Jun 15 '17 at 22:56
-
The plugin will only trigger (and reformat) when you switch windows. If it happens more often then that, it is because another plugin interfere with the save mechanism (Eclipse formatter for example). Open an issue on https://github.com/dubreuia/intellij-plugin-save-actions/issues with installed plugins and I'll try to correct the problem by filtering save event (I'm the plugin dev). – Alexandre DuBreuil Jun 28 '17 at 15:06
-
1I'm slowly adding new products (Android Studio, PyCharm, etc.), you can use a beta version and follow advancement here https://github.com/dubreuia/intellij-plugin-save-actions/issues/18 (I'm the plugin dev) – Alexandre DuBreuil Jun 28 '17 at 15:08
-
Unfortunately, this plugin does not perform a complete "cleanup" action, so not sufficient for anything except including inspections auto-fix and simple re-formatting. – Dmitriy Popov Sep 17 '19 at 11:13
Below is Neil's answer updated.
IntelliJ 13 Steps:
- Code -> Reformat Code
- Edit -> Macros -> Start Macro Recording
- Code -> Reformat Code
- File -> Save all
- Edit -> Macros -> Stop Macro Recording
- Name the macro (something like "formatted save")
- File -> Settings -> Keymap
- Right click on the macro. Add Keyboard Shortcut. Set the keyboard shortcut to Control + S.
- IntelliJ will inform you of a hotkey conflict. Select "remove" to remove other assignments.

- 4,214
- 3
- 36
- 55

- 951
- 6
- 3
-
1But this removes the "Save" action. What the poster has seen is that in Eclipse we have the option to format code and organize import automatically when a file is saved. This option is (I assume) absent in IntelliJ idea. This is a work around which involves binding the save key to the reformat key. – Ganesh Krishnan Sep 17 '15 at 04:36
-
3I disagree, @GaneshKrishnan. This workaround does not bind the "save key" to the "reformat key". It binds it to a macro that "reformats *and* saves", which is exactly what was asked for by OP and what is implemented in Eclipse, but triggered with a check box. – dbm Aug 23 '16 at 07:20
-
1I think maybe for step 1 you mean "Code -> Show Reformat File Dialog" to set the defaults for Reformat File Dialog (so that they are set correctly for step 3 - Reformat Code)? – studgeek Jan 20 '17 at 15:45
I set it to automatically clean up on check-in, which is usually good enough for me. If something is too ugly, I'll just hit the shortcut (Ctrl-Alt-L, Return). And I see they have an option for auto-formatting pasted code, although I've never used that.

- 4,903
- 5
- 32
- 43

- 3,573
- 5
- 26
- 25
-
12You can also use Ctrl-Shift-F to format. That works way better for me, since Ctrl-Alt-L is the lock screen command on Ubuntu. – Jason McVetta May 14 '13 at 19:25
-
24
-
7
-
3Does that work if you are not checking in using intelliJ i.e I think it won't work if you checking in using command line tool. – Sep 10 '15 at 09:43
-
3
-
@Ben it doesn't, but it was enough for OPs problem problem, even tho he asked about a specific way f solving it - which was not answered. – BlueWizard Dec 08 '16 at 09:41
If you have InteliJ Idea Community 2018.2 and above the steps are as fallows:
- In the top menu you click: Edit > Macros > Start Macro Recordings (you'll see a window lower right corner of your screen confirming that macros are being recorded)
- In the top menu you click: Code > Reformat Code (you'll see the option being selected in the lower right corner)
- In the top menu you click: Code > Optimize Imports (you'll see the option being selected in the lower right corner)
- In the top menu you click: File > Save All
- In the top menu you click: Edit > Macros > Stop Macro Recording
- You name the macro: "Format Code, Organize Imports, Save"
- In the top menu you clock: File > Settings. In the settings windows you click Keymap
- In the search box on the right you search "save". You'll find Save All (Ctrl+S). Right click on it and select "Remove Ctrl+S"
- Remove your search text from the box, press on the Collapse All button (Second button from the top left)
- Go to macros, press on the arrow to expand your macros, find your saved macro and right click on it. Select Add Keyboard Shortcut, and press Ctrl+S and okay.
Restart your IDE and try it.
I know what you're going to say, the guys before me wrote the same thing. But I got confused using the steps above this post, and I wanted to write a dumb down version for people who have the latest version of the IDE.

- 1,257
- 13
- 24
Rejoice! In IDEA 2021.2 there is finally "File->Settings->Tools->Actions on Save" where you can select "Reformat code", "Optimize imports", "Rearrange code", "Run code cleanup", "Run eslint --fix" etc.

- 6,000
- 2
- 35
- 25
-
This should be the accepted answer at this stage. The macros approach works fine but this is the best option with the latest IntelliJ feature set. – Brendan Maguire Apr 19 '23 at 11:25
Ctrl + Alt + L is format file (includes the two below)
Ctrl + Alt + O is optimize imports
Ctrl + Alt + I will fix indentation on a particular line
I usually run Ctrl + Alt + L a few times before committing my work. I'd rather it do the cleanup/reformatting at my command instead of automatically.
-
-
13Careful on Linux as `Ctrl + Alt + L` is commonly Lock Session. You'd need to remap one of the functions. – indivisible Jun 15 '14 at 04:16
-
The Ctrl-Alt-L key can be unbound in Ubuntu. https://stackoverflow.com/q/24908490/53897 – Thorbjørn Ravn Andersen Dec 27 '20 at 03:21
-
If you're developing in Flutter, there's a new experimental option as of 5/1/2018 that allows you to format code on save.

- 3,189
- 4
- 26
- 34
I wound up rebinding the Reformat code...
action to Ctrl-S, replacing the default binding for Save All
.
It may sound crazy at first, but IntelliJ seems to save on virtually every action: running tests, building the project, even when closing an editor tab. I have a habit of hitting Ctrl-S pretty often, so this actually works quite well for me. It's certainly easier to type than the default bind for reformatting.
IntellIJ 14 && 15: When you are checking in code in Commit changes dialog, tick the Reformat code checkbox, then IntelliJ will reformatting all the code that you are checking in.
Source: www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours

- 13,575
- 26
- 81
- 144

- 129
- 2
- 1
-
2What did I said [here](http://stackoverflow.com/questions/3754333/what-is-the-shortcut-in-intellij-idea-to-find-method-functions/36825075#comment61221258_36825075) – Tushar Apr 24 '16 at 15:37
-
5Beware that excessive promotion of one resource may be interpreted by the community as spam. You should post good, relevant answers, and if one or two happen to mention this course, that's fine - but if you're posting it in every answer, it's going to get flagged and deleted. – ArtOfCode Apr 24 '16 at 15:38
For PyCharm/IntelliJ IDEA:
- Install black.
$ pip install black
- Locate your black installation folder.
On macOS / Linux / BSD:
$ which black
/usr/local/bin/black # possible location
On Windows:
$ where black
%LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
Note that if you are using a virtual environment detected by PyCharm, this is an unneeded step. In this case the path to black is $PyInterpreterDirectory$/black
.
- Open External tools in PyCharm/IntelliJ IDEA
On macOS: PyCharm -> Preferences -> Tools -> External Tools
On Windows / Linux / BSD: File -> Settings -> Tools -> External Tools
- Click the + icon to add a new external tool with the following values:
Name: Black
Description: Black is the uncompromising Python code formatter.
Program: <install_location_from_step_2>
Arguments: "$FilePath$"
- Format the currently opened file by selecting Tools -> External Tools -> black.
Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Black.
- Optionally, run Black on every file save:
Make sure you have the File Watchers plugin installed.
Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:
Name: Black
File type: Python
Scope: Project Files
Program: <install_location_from_step_2>
Arguments: $FilePath$
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$
Uncheck “Auto-save edited files to trigger the watcher” in Advanced Options
To format Python files with Black, I followed this guide, which also uses File Watcher: https://black.readthedocs.io/en/stable/editor_integration.html

- 527
- 5
- 5
Since version 2020.1, you can activate Run on save for files directly in the Preferences of the Prettier plugin:

- 54,283
- 17
- 197
- 195
-
1
-
@Farid IntelliJ does support JavaScript projects though and so this answer is apparently helpful to other people. The question does not ask for a solution that covers all possible cases nor states a specific case/language. – Kim Kern Nov 27 '21 at 23:39
If it's about Prettier, just use a File Watcher :
references => Tools => File Watchers => click + to add a new watcher => Prettier
https://prettier.io/docs/en/webstorm.html#running-prettier-on-save-using-file-watcher

- 3,295
- 1
- 20
- 39
-
3Prettier is great but it doesn't work for languages/formats outside of the web. It'll format js, ts, css, json, etc. but is useless for C, C#, Java, Python, etc. - with the exception of some early attempts at plugins. – Mattia Oct 21 '18 at 14:30
I thought there was something like that in IntelliJ, but I can't find it. The only clean-up that happens at save is that white space at the ends of lines is removed. I thought I had to specify that behavior at one point, but I don't see anything related at this point.

- 2,688
- 1
- 18
- 16