How to enable automatic code format for Intellij IDEA?

Is this possible in Intellij IDEA after entering ';' or press 'Enter' did this line format?

For example, in:

a+b=10; 

after

 a + b = 10; 

Or just a possible option: Code> Reformat Code ...?

Thank!

+87
intellij-idea format reformat
Jul 26 '13 at 10:51 on
source share
12 answers

Shortcut autoformatting in IntelliJ

  • For windows Ctrl + Alt + L.
  • For Ubuntu Ctrl + Alt + windows + L.
  • For Mac Command + Option + L.
+118
Jul 26 '13 at 11:23
source share

I found two ways to do this:

  • Go to Settings> Keymap. On the right side, go to Editor Actions> complete current statement. Click on it and select add a keyboard shortcut. Click; and select ok.

  • Use a macro. Switch to

Edit> Macros> Start Macro Recording.

Now press the semicolon and the keyboard to reformat the code (you can find the key combination from other answers or from the settings> keyboard layout).

After formatting, go to

Edit> Macros> Stop Macro Recording

Save the macro with a name (automatic format or something else). Then go to

Settings> Keymap> Macros> auto format (macro name).

Click there and select add a keyboard shortcut, then click the semicolon and click OK. Now, when you click on a semicolon, it will write a semicolon and do an automatic format.

+13
Nov 04 '17 at 9:33
source share

Eclipse has the ability to automatically format when saving a file. IntelliJ does not have the ability, although you can configure the macro for the Ctrl + S keys (Cmd + S on Mac) to format the code and save it.

Intellij reformat when saving a file

+12
Jun 16 '15 at 9:44
source share

The default value is Ctrl + Alt + L. There is a key mapping dialog where you can configure / override all keyboard shortcuts.

Go to File-> Settings-> KeyMap

enter image description here

+6
Dec 19 '16 at 13:50
source share

File-> Preferences β†’ Keymap-> End Current Report

I have added ; the key is there. When typing ';' automatic formatting occurs at the end of the line.

UPDATE

I realized that this would cause some problems in some situations. Use Ctrl + Shift + Enter instead. This key can be used at any cursor position in a line. This will add ; at the end of the line. This shortcut also has some other useful features.

like:

 public void someMethod() 

after label:

 public void someMethod() { // The cursor is here } 

therefore formatting when pasting ; not necessary.

+6
Feb 09 '17 at 15:46
source share

This can also be achieved with Ctrl + WindowsBtn + Alt + L. This will be important for some people, because in some virtual machines Ctrl + Alt + L can log you out of the system.

+4
Jan 28 '16 at 9:32
source share
Combination

Ctrl + Shift + Enter (Complete Statement) also does the job and does some more useful functions with functions, loops, etc.

JetBrains docs: https://www.jetbrains.com/help/idea/2016.3/completing-statements.html

+4
Mar 05 '17 at 18:25
source share

On a Mac, this is Alt + Command + L (assuming you haven't changed any modifiers or Intellij keyboard shortcuts from its default state)

+2
Aug 27 '17 at 9:05 on
source share

None of the solutions in Intellij are as elegant (or useful) as in Eclipse. What we need is a function request to intellij so that we can add a trap (what actions to perform) when the IDE is automatically saved.

In Eclipse, we can add post-save actions, such as ordering the import and formatting the class. Yes, you have to do "save" or ctrl-s, but the trap is very convenient.

+2
Sep 28 '18 at 12:19
source share

Well, that’s not possible, but in intellij 13, how about adding mouse gestures, something like a single left click to reformat the code? Or if you don’t use a mouse, then add a very simple hotkey combination that you use all the time (maybe "enter"? Not sure if intellij would be happy with that, to be honest)

+1
Feb 26 '14 at 10:20
source share

The way I implemented automatic reformatting, as in Microsoft Visual Studio (it does not work perfectly):

 1. Edit > Macros > Start Macro Recording 2. Press continuously: Enter + Ctrl+Alt+I 3. Edit > Macros > Stop Macro Recording (Name it for example ReformatingByEnter) 

Now we need to perform the same actions, but for Ctrl + Alt + L +;

 4. Edit > Macros > Start Macro Recording 5. Press continuously: ; + Ctrl+Alt+I 6. Edit > Macros > Stop Macro Recording (Name it for example ReformatingBy;) 

Now we need to assign hotkeys to these macros:

 7. File > Settings > Keymap > press on a gear button > Duplicate... 8. Unfold a macro button (beneath) and by right clicking on two ours macros assign HotKeys for them: "Enter" and ";" correspondingly. 
+1
Jan 28 '18 at 22:22
source share

According to Larson's comment, this is now possible with the save-actions plugin.

0
May 02 '19 at
source share



All Articles