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.