66

How can I set up that the code is reformatted automatically on saving?

So I don't always have to press Ctrl + Alt + L.

Amin Soheyli
  • 605
  • 2
  • 7
  • 16
ndrizza
  • 3,285
  • 6
  • 27
  • 41
  • 3
    The accepted answer is fine, but probably better for most people to use the save actions plugin https://stackoverflow.com/questions/946993/intellij-reformat-on-file-save – CodeMonkey Jul 19 '18 at 11:03

6 Answers6

134

Actually it is possible.
The idea is to record a very simple macro that selects all the lines (Ctrl+A), formats (Ctrl+Alt+L), deselect all (Up and Down arrow) and saves a file (Ctrl+S), and bind this macro to Ctrl+S.

Free Ctrl+S changing keyboard shortcut for "Save all"

  1. open File > Settings;
  2. search for "keymap" and open it;
  3. search "Save All" and double click the action "Save all";
  4. select "Remove Ctrl+S";
  5. double click "Save All" again and select "Add Keyboard Shortcut";
  6. select "Ctrl+Alt+Shift+S" as first stroke.
  7. Confirm and exit from Settings.

Record the macro

  1. Edit > Macros > Start Macro recording
  2. Press Ctrl+A, then Ctrl+Alt+L, then Up arrow, then Down arrow, and finally then Ctrl+Alt+Shift+S
  3. Stop recording the macro clicking on the Stop button on the bottom right of the page.
  4. Give this macro a name like "Format and Save"

Assign Ctrl+S to "Format and Save"

  1. open File > Settings;
  2. search for "keymap" and open it;
  3. search "Format and Save" and double click the action "Format and Save";
  4. select "Add Keyboard Shortcut";
  5. select "Ctrl+S" as first stroke.
  6. Confirm and exit from Settings.

Finally, enjoy PhpStorm!

Shivang Patel
  • 157
  • 1
  • 3
  • 13
Luca Mastrostefano
  • 3,201
  • 2
  • 27
  • 34
  • 28
    The macro doesn't have to be that long: Ctrl+Alt+L and Ctrl+Alt+S does the same thing as well. – Saugat May 26 '15 at 15:44
  • 5
    Please replace "Ctrl+Alt+S" with "Ctrl+Alt+Shift+S" as the latter has no conflicts. – goyote Mar 29 '17 at 11:14
  • 1
    **Useful hint to format all directory files:** First select the directory in `Project` window and then from `code` menu, choose `Reformat code` – R Sun Sep 30 '18 at 10:50
  • 1
    would this work with phpstorm's auto saving feature as well? As in it would auto save + auto format without clicking any hotkeys? – Dr. House May 18 '20 at 23:44
  • PhpStorm refuses to correctly indent my lines. For example, if I mash tab and indent a line 10 times, it doesn't snap back to where it's supposed to be. I can't find out how to fix this formatting, help! – CoredusK Sep 30 '20 at 10:04
  • @Mkey i tested this phpstorm 2020.3 not working with autosave + auto format without clicking any hotkeys. :( – Subramanya Rao Dec 26 '20 at 15:28
  • The fact that PHP Storm doesn't have a simple "format on save" preference (and the ability to map formatters for each language) is MIND BOGGLING. Use VS Code. – atwixtor Apr 29 '22 at 19:19
22

I use Save Actions Plugin https://plugins.jetbrains.com/plugin/7642-save-actions for PhpSorm

es cologne
  • 3,318
  • 2
  • 17
  • 11
14

Settings -> Tools -> Action on Save -> Reformat code

6

Another thing that I found very helpful in combination with this macro is the

Show Reformat File Dialog.

In case you want to only reformat the code you just changed, you can set the option:

Only VCS changed text

The dialog can be opened through the following shortcut: CTRL+ALT+SHIFT+L

Timothy
  • 61
  • 1
  • 2
1

Checked on PHPStorm 2020 & greater versions and found a plugin named Save Action. But make sure you configure for new and exisiting projects.

You can also export your PHPStorm settings and then unzip & replace/create the following :-

  1. Macro File under Settings/Option/macro.xml
    <application>
    <component name="ActionMacroManager">
    <macro name="Format & Save">
    <action id="ReformatCode"/>
    <action id="SaveAll"/>
    </macro>
    </component>
    </application>
  1. Settings/Keymaps/Default Copy.xml
    <keymap version="1" name="Default copy" parent="Default for XWin">
    <action id="DBNavigator.Actions.SourceEditor.Save"/>
    <action id="Macro.Format & Save">
    <keyboard-shortcut first-keystroke="ctrl s"/>
    </action>
    <action id="SaveAll">
    <keyboard-shortcut first-keystroke="shift ctrl alt s"/>
    </action>
    </keymap>

Now Zip it back & import it & whenever you press ctrl+s - Auto Code Format will be done.

Basant Mandal
  • 96
  • 1
  • 3
0

From v2017.2.4 CTRL + ALT + L doesn't work. Use alt + F8 instead.

Solution: Use: ALT + F8 to format the code.

When creating a macro as described by Luca Mastrostefano, use: alt + F8 to format and then ctrl + alt + s to save.

Pav K.
  • 2,548
  • 2
  • 19
  • 29