This solution worked better for me:

  1. Make a macro (I used Organize Imports, Format Code, Save All)
  2. 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:

  1. Code -> "Optimize Imports...", if a dialogue box appears, check the box that says "Do not show this message again.", then click "Run".
  2. Tools -> "Start Macro Recording"
  3. Code -> "Optimize Imports..."
  4. Code -> "Reformat Code..."
  5. File -> "Save all"
  6. Tools -> "Stop Macro Recording"
  7. Name the macro (something like "formatted save")
  8. In File -> Settings -> Keymap, select your macro located at "Main Menu -> Tools -> "formatted save"
  9. 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.
  10. 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.

Answer from Neil on Stack Overflow
🌐
JetBrains
jetbrains.com › help › idea › reformat-and-rearrange-code.html
Reformat code | IntelliJ IDEA Documentation
3 weeks ago - Automatic indent detection can override the indents that you configure on the Tabs and Indents tab. In this case, IntelliJ IDEA displays a notification. You can configure the IDE to reformat code in modified files automatically when your changes are saved.
Top answer
1 of 15
474

This solution worked better for me:

  1. Make a macro (I used Organize Imports, Format Code, Save All)
  2. 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:

  1. Code -> "Optimize Imports...", if a dialogue box appears, check the box that says "Do not show this message again.", then click "Run".
  2. Tools -> "Start Macro Recording"
  3. Code -> "Optimize Imports..."
  4. Code -> "Reformat Code..."
  5. File -> "Save all"
  6. Tools -> "Stop Macro Recording"
  7. Name the macro (something like "formatted save")
  8. In File -> Settings -> Keymap, select your macro located at "Main Menu -> Tools -> "formatted save"
  9. 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.
  10. 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.

2 of 15
336

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.

Discussions

IntelliJ IDEA: How to set auto format on file save JAVA - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
ide - Disable reformatting code when saving files - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I made a small change to an old java file in IntelliJ and when I save the file the IDE automatically reformats all the code. More on stackoverflow.com
🌐 stackoverflow.com
Request: for "Reformat file" on save, please prevent it in case it changes logic or when the code has errors
Describe the solution you'd like Suppose I have these settings: On Java and especially on Kotlin, when I leave the IDE in the background (pressing Home key, for example), it sometimes auto-form... More on github.com
🌐 github.com
10
May 10, 2023
How enable auto-format code for Intellij IDEA? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. Is it possible in Intellij IDEA after typing ; or at any event formatting of this string happens automatically? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/intellijidea › intellij finally has a built in "reformat code and organize imports" save action in 2021.2
r/IntelliJIDEA on Reddit: Intellij finally has a built in "Reformat code and organize imports" save action in 2021.2
July 28, 2021 -

Intellij finally implemented a feature that other IDEs had for years. You can now finally set up Intellij to automatically reformat your code and organize your imports on each save. There is no more need to either download the "Save Actions plugin" or to write your own macro to execute these two commands on each save. It was about effin' time.

🌐
Medium
hirosht.medium.com › automatically-reformat-code-on-save-in-intellij-ddf7842801e0
Automatically reformat code on save in IntelliJ - Hirosh Tharaka - Medium
July 12, 2023 - Automatically reformat code on save in IntelliJ Configure the IDE to reformat code in modified files automatically when your changes are saved. Press Ctrl+Alt+S to open the IDE settings select Tools …
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 207060945-Format-Code-on-Save
Format Code on Save – IDEs Support (IntelliJ Platform) | JetBrains
October 2, 2012 - I just use a shell script to automatically run the following python script in separate terminal when i start pycharm or another intellij IDE. import pyautogui import time while True: time.sleep(600) pyautogui.hotkey('ctrl', 'alt', 'l') # or the hotkey for another os print("triggered") # lower sleep as you need · in terms of productivity and practicality, this is the simplest, least intrusive, and most automated method I've personally found thus far. Sure it might run when I'm in another window once in a while, and uses up some memory, but for my work its not an issue.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360010666599-Auto-format-indent-of-code
Auto format/indent of code – IDEs Support (IntelliJ Platform) | JetBrains
May 10, 2021 - It's also pretty easy to configure and use (Preferences / Settings | Save Actions). Thanks for your feedback. We currently investigate what our users prefer to use (GO-10404). If you're interested in why gofmt on save isn't enabled by default ...
🌐
JetBrains
jetbrains.com › help › rider › Enforcing_Code_Formatting_Rules.html
Apply formatting rules | JetBrains Rider Documentation
You can apply formatting preferences every time you save changes in a file to make sure that your edits always comply with your code style. Note that this will only happen when you save changes explicitly with Ctrl+S or Ctrl+S and will not be triggered by auto-saving.
Find elsewhere
🌐
Medium
medium.com › nerd-for-tech › auto-format-code-in-android-studio-intellij-idea-1f0450ee44a3
Format java kotlin code automatically after saving a file using Intellij IDEA | Medium
June 10, 2021 - You can always reassign it later if necessary. Click OK to apply the changes. Now, when you press ⌘S, Android Studio/IntelliJ IDEA will invoke the new macro: reformat the current file and save your project.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 4571733653650-Reformat-on-Save-is-not-Working
Reformat on Save is not Working – IDEs Support (IntelliJ Platform) | JetBrains
March 9, 2022 - I am working with Java files. Update - I use the plugin Adapter for Eclipse Code Formatter. Disabling it, restarting IntelliJ, then enabling it and restarting IntelliJ fixed the issue.
🌐
JetBrains
jetbrains.com › help › idea › prettier.html
Prettier | IntelliJ IDEA Documentation
1 month ago - To set Prettier as the default formatting tool, select the Run on 'Reformat Code' action checkbox. To apply Prettier automatically to saved files, select the Run on save checkbox.
🌐
tutorialpedia
tutorialpedia.org › blog › how-to-disable-intellij-auto-change-my-code-format
How to Disable IntelliJ Auto Formatting: Prevent Unwanted Code Format Changes Beyond Edited Areas in Java — tutorialpedia.org
Here’s how to disable it: ... Open IntelliJ and go to File > Settings (Windows/Linux) or IntelliJ IDEA > Preferences (macOS). Navigate to Tools > Actions on Save. ... Uncheck the box next to "Reformat code" to disable automatic formatting on save.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206245369-Autoformat-on-Save
Autoformat on Save – IDEs Support (IntelliJ Platform) | JetBrains
August 11, 2011 - Do you mean 'reformat' under 'autoreformat'? Please provide your code style settings '*.xml' files (contents of the 'codestyles' folder under 'config dir').
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206229129-Auto-format-in-IntelliJ-IDEA
Auto format in IntelliJ IDEA? – IDEs Support (IntelliJ Platform) | JetBrains
May 23, 2012 - There is no "Autoformat on save" option, since save is performed implicitly in IntelliJ IDEA, and we don't use it as a way to trigger actions. There is a "Reformat on commit" option, though.
🌐
JetBrains
blog.jetbrains.com › home › intellij idea › how to format java code
How to Format Java Code - The JetBrains Blog
June 6, 2024 - Under ‘for()’ statement in the Wrapping and Braces tab, we set Force braces to Always so IntelliJ IDEA will always insert curly braces around the body of our for loops. When we save these changes, they will be saved to our current Code Style scheme. Often we don’t want to reformat the whole file. If we were working on a file with some inconsistently formatted code, but we’ve only changed a small part of it, running Reformat Code might change parts of the file we haven’t touched.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 13134493544850-Reformat-On-Save-doesn-t-work
Reformat On Save doesn't work – IDEs Support (IntelliJ Platform) | JetBrains
August 17, 2023 - Could you please try to run IDE with default settings ("File | Manage IDE Settings | Restore Default settings"), then configure “On Save” actions to “Reformat” only and check the issue on Java file and JavaScript file?
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 18703901582994-Auto-format-on-the-fly-not-using-the-same-rules-as-format-on-save
Auto-format on the fly not using the same rules as format on save – IDEs Support (IntelliJ Platform) | JetBrains
May 2, 2024 - Basically, I have my formatter set to scalafmt, and if I manually do cmd-s, the file is formatted as I want - specifically, the imports are grouped correctly and don't exceed the line width limit. However, the auto-saving uses some other formatting rules that screw up all the imports. I _want_ auto-save, and I'd be happy to have auto-formatting on autosave _if_ it used the correct rules.
🌐
GitHub
github.com › dubreuia › intellij-plugin-save-actions › issues › 430
Request: for "Reformat file" on save, please prevent it in case it changes logic or when the code has errors · Issue #430 · dubreuia/intellij-plugin-save-actions
May 10, 2023 - Request: for "Reformat file" on save, please prevent it in case it changes logic or when the code has errors#430 ... On Java and especially on Kotlin, when I leave the IDE in the background (pressing Home key, for example), it sometimes auto-formats when it shouldn't.
Author: dubreuia