86

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Pavel Bernshtam
  • 4,232
  • 8
  • 38
  • 62
  • 2
    possible duplicate of [Intellij reformat on file save](http://stackoverflow.com/questions/946993/intellij-reformat-on-file-save) – Romain Linsolas Nov 08 '10 at 17:22
  • 1
    I complelty agree that it is a missing (and so basic) feature in IntelliJ. Another one is the intelligent insertion of `{` and `;` in Eclipse (*Java > Editor > Typing > Automatically insert at correct position*) – Romain Linsolas Nov 08 '10 at 17:23

8 Answers8

73

You will need to Install Save Actions plugin for Intellij IDEA

  1. Click: ctrl+shift+a
  2. Type: Plugins
  3. Click: enter Plugins
  4. Click: Browse repositories
  5. Type: Save Actions to search field
  6. Click "install plugin" after you found it
  7. Restart intellij idea

Save actions now work. You can configure them in File -> Settings -> Other settings

Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
justnpT
  • 1,039
  • 9
  • 7
  • 5
    I tried the plugin "Save Actions" from Alexandre DuBreuil and it does really what I've expected (same behavior as Eclipse save actions). I can recommend it. – Julian L. Jan 29 '16 at 20:35
  • 6
    This addon is mostly for Java developers, you can't really configure anything special (like run command x, external tool y etc.). Just a note. – fgblomqvist May 10 '16 at 00:22
  • 2
    Thanks for using my plugin! Any issues with it goes there: https://github.com/dubreuia/intellij-plugin-save-actions/issues – Alexandre DuBreuil Jun 28 '17 at 15:10
  • 1
    Does anyone of u knows, how to make the saveAction plugin configuration applied to all projects – Ikbel Aug 26 '21 at 09:56
  • 2
    in V 2021.2.2 this has become a built in feature in `File | Settings | Tools | Actions on Save` – elonderin Oct 08 '21 at 12:47
16

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

Michael Barfs
  • 336
  • 4
  • 10
  • This option is only available on each project (once opened, File > Settings) but not from the general start screen (Customize > All settings...). Can't confirm if this works for all projects once set or not. – lucasvc Dec 16 '22 at 17:20
  • Also the "Actions on Save" do not support custom actions. – Marvin Jan 25 '23 at 15:47
10

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 :

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 21
    The point of this question is that Eclipse does it automatically so you literally don't have to think about it compared to IntelliJ where you have to develop a reflex. I agree with the question that this is something IntelliJ really needs to have – Richard Dec 13 '11 at 13:46
4

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!

GabrielWeis
  • 340
  • 1
  • 3
  • 13
  • I find this feature almost useless, as macros in Intellij even in latest 15 version don't record timings, which are very important when you say chain frontend build to backend resource reload. So intellij plays macro actions simultaneously :-\ – Aubergine Jan 03 '16 at 07:37
2

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.

manuna
  • 729
  • 14
  • 37
1

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).

Community
  • 1
  • 1
stackular
  • 1,431
  • 1
  • 19
  • 41
1

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.

jarzec
  • 459
  • 4
  • 8
0

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.

osundblad
  • 2,675
  • 1
  • 29
  • 34
  • 1
    I think the save file thing contradicts the "nothing should be done unless the developer requests it philosophy". Also the actions that can be run automatically on commit do not follow this paradigm. – Roland Nov 15 '16 at 13:36
  • "I think it might break the philosophy that JetBrains have" Nah they already made this build-in function – Farid Nov 27 '21 at 19:59