Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code reformatting on save in PhpStorm or other jetbrains ide

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

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

like image 264
ndrizza Avatar asked Sep 19 '12 14:09

ndrizza


People also ask

How do I enable auto format in IntelliJ?

Automatically formatting code in Android Studio and IntelliJInstall the Dart plugin (see Editor setup) to get automatic formatting of code in Android Studio and IntelliJ. To automatically format your code in the current source code window, use Cmd+Alt+L (on Mac) or Ctrl+Alt+L (on Windows and Linux).

What does rearrange code do in IntelliJ?

Rearrange code just does the 1st point, arranges it according to android code style guidelines. Reformat code does both 1st and 2nd point. Show activity on this post. Rearranging code does more things than just reformat the code.

What is reformat code in PyCharm?

Tell PyCharm to clean up indentation and other code style in your file.


2 Answers

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!

like image 134
Luca Mastrostefano Avatar answered Sep 27 '22 19:09

Luca Mastrostefano


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

like image 25
es cologne Avatar answered Sep 27 '22 21:09

es cologne