Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto format code in WebStorm?

I'm looking for a way to auto-format my code in the WebStorm IDE?

like image 959
HasanAboShally Avatar asked Jan 19 '14 14:01

HasanAboShally


People also ask

How do I format a code in WebStorm?

In the editor, select a code fragment you want to reformat. Before reformatting, you can take a look at the code style settings that are applied to the selected code: press Alt+Enter and click Adjust code style settings. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L .

How do I refactor code in WebStorm?

Press F6 or choose Refactor | Move from the context menu or from the main menu. Alternatively, choose Refactor | Refactor This or press Ctrl+Alt+Shift+T , then choose Move from the list. The Move Module Members dialog opens. Specify the destination file and select the members to move.

How do I format a code automatically in IntelliJ?

If you choose Code | Reformat Code from the main menu or press Ctrl+Alt+L , IntelliJ IDEA tries to reformat the source code of the specified scope automatically.


2 Answers

Mac: Command + Option + L

PC: Ctrl + Alt + L

like image 179
HasanAboShally Avatar answered Sep 18 '22 17:09

HasanAboShally


It is possible by creating a macro that formats the code and save it, and then bind this macro to Ctrl+S shortcut. (Mac: Cmd+S)

This answer was posted for phpStorm and it is equally valid for WebStorm. A modified version of answer is as follows:


Record the macro

  1. Edit > Macros > Start Macro recording
  2. Press Ctrl+Alt+L, and then Ctrl+Alt+S (on Mac: Cmd+Option+L, and then Cmd+Option+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. it will report conflicts. Ignore it and click OK button
  7. WebStorm will show a warning "The shortcut is already assigned to other actions. Do you want to remove other assignments?" Click "Remove" button

That's it.

like image 37
Andrew-Dufresne Avatar answered Sep 21 '22 17:09

Andrew-Dufresne