Save actions in IntelliJ IDEA
Intellij IdeaIntellij Idea Problem Overview
The only thing that exists in Eclipse, which I miss in IntelliJ IDEA is "save actions" - possibility to do some actions (usually formatting) when a file is saved.
Is there some plugin or configuration for IDEA which enables such functionality?
Intellij Idea Solutions
Solution 1 - Intellij Idea
You will need to Install Save Actions plugin for Intellij IDEA
- Click: ctrl+shift+a
- Type: Plugins
- Click: enter Plugins
- Click: Browse repositories
- Type: Save Actions to search field
- Click "install plugin" after you found it
- Restart intellij idea
Save actions now work. You can configure them in File -> Settings -> Other settings
Solution 2 - Intellij Idea
UPDATE: Since IntelliJ Version 2021.2 Jetbrains added support for Actions on Save. They can be found under Preferences/Settings | Tools | Actions on Save.
Source: https://blog.jetbrains.com/idea/2021/07/intellij-idea-2021-2/#key_updates
Solution 3 - Intellij Idea
It doesn't exist as in Eclipse, but you can format your code on commit, and the shortcut ctrl+alt+L will allow you to format easily your code.
After a time formatting the code will be a reflex and you won't even think about it.
On the same topic :
Solution 4 - Intellij Idea
You may use a Macro to simulate save actions:
Edit --> Macros --> Start Macro Recording
==> now execute all actions you need (e.g. reformat, rearrange, optimize imports)
Edit --> Macros --> Stop Macro Recording
Give your new Macro a name. You can also assign a shortcut to your macro. Just enter it's name in the settings search and you will find it under Macro.
Have fun!
Solution 5 - Intellij Idea
There is something similar - possibility to do some actions, when a file is committed. In a file commit window there is a "Before commit" section which contains checkboxes like "optimize imports", "Reformat code", etc.
Solution 6 - Intellij Idea
See here: Intellij reformat on file save.
The answer I linked to explains in a few easy steps how to record a macro for optimize imports and reformat and then run that macro when you save your files (bind it to Ctrl+S
).
Solution 7 - Intellij Idea
Had the same problem, but Save Actions were not flexible enough. I went for File Watchers plugin which allows for running custom programs each time a file of a given type changes.
Solution 8 - Intellij Idea
I think it might break the philosophy that JetBrains have. Nothing should be done unless the user/developer requests it (and no Wizards). So you are stuck with Ctrl-Alt-L or just indent Ctrl-Alt-I. I personally only use the latter since the code usually formats itself as you type.
Also Idea saves files as soon as you leave window or (I think) wait for a certain time, this might really mess up things.