How enable auto-format code for Intellij IDEA?
Asked Answered
T

16

176

Is it possible in Intellij IDEA after typing ; or at any event formatting of this string happens automatically?

For instance, to:

a+b=10;

after:

a + b = 10;

Or only possible option: Code > Reformat Code...?

Thank you!

Traject answered 26/7, 2013 at 10:51 Comment(2)
Alt + c + rLathy
if you would like for this feature to be included in IntelliJ itself (not in any plugin), please vote here: youtrack.jetbrains.net/issue/IDEABKL-5806Pardon
J
233

The formatting shortcuts in Intellij IDEA are :

  • For Windows : Ctrl + Alt + L
  • For Ubuntu : Ctrl + Alt + Windows + L
  • For Mac : ⌥ (Option) + ⌘ (Command) + L
Jaret answered 26/7, 2013 at 11:23 Comment(7)
Yes, but I would like this to happen automatically without any of pressing a hotkey.Traject
Macro? ctrl+alt+l becomes ctrl+alt+l+enterBrocky
The question expressly state "auto-format" after each line is written.Roadside
To simplify reformatting on Ctrl+Alt+L, you can open Settings - Editor - General and remove check mark from "Show 'Reformat Code' dialoog" check box in the Formatting section.Selfconfidence
@Jaret is there a way to do this automatically when saving or compiling the codeAmylaceous
That does not answer the question. The answer by Nafiur Rahman Khadem does.Gaultiero
This answer links to a plugin that allows such function #947493Wray
S
38

You can do this with Save Actions plugin Refer This article on how to configure the plugin.

Save Actions plugin Supports configurable, Eclipse like, save actions, including "optimize imports", "reformat code", "rearrange code", "compile file" and some quick fixes for Java like "add / remove 'this' qualifier", etc. The plugin executes the configured actions when the file is synchronised (or saved) on disk.

  1. Install the plugin
  2. Go to Settings > Other settings > Save actions
  3. Tick Activate save actions on save
  4. Tick Reformat file
  5. Apply > OK

enter image description here

I prefer the hot-keys though, For Mac,

To format the code : Ctrl+Alt(Option)+L

And additionally I do,: Ctrl+Alt(Option)+O , This will remove unused imports and format the import list as well.

Stiver answered 12/6, 2020 at 17:55 Comment(0)
T
28

I have found two ways to do this:

  1. Go to Settings> Keymap.In the right portion go to Editor Actions> complete current statement.Click on it and select add keyboard shortcut. Press ; and select ok.

  2. Use macro. Go to

Edit> Macros> Start Macro Recording.

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

After doing reformat go to

Edit> Macros> Stop Macro Recording

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

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

Click there and select add keyboard shortcut, then press semicolon and click ok. Now whenever you will press semicolon it will write semicolon and do auto format.

Tessi answered 4/11, 2017 at 9:33 Comment(2)
Nice. I also added Shift+[ using the first method to format the line when writing if statements and methods and such. For some reason, Enter didn't seem to work, though.Devious
This was very helpful, it's weird that Intellij doesn't have auto-reformat option at allAnkney
P
13

Eclipse has an option to format automatically when saving the file. There is no option for this in IntelliJ although you can configure a macro for the Ctrl+S (Cmd+S on Mac) keys to format the code and save it.

Intellij reformat on file save

Postfree answered 16/6, 2015 at 9:44 Comment(0)
M
7

File-> Settings -> Keymap-> Complete Current Statement

I added ; key in there. When typing ';' at the end of the line, it is auto-formatting.

UPDATE

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

like:

public void someMethod()

after shortcut:

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

so formatting on inserting ; is not necessary.

Maduro answered 9/2, 2017 at 15:46 Comment(1)
Smart, but not for KotlinShortridge
G
6

This can also be achieved by Ctrl+WindowsBtn+Alt+L. This will be important to some people,because in some Virtual Machines, Ctrl+Alt+L can log you out.

Gilmore answered 28/1, 2016 at 9:32 Comment(0)
B
6

Default one is Ctrl+Alt+L There is a key-mapping dialog box where you can configure/overwride all keyboard shortcuts.

Navigate File->Settings->KeyMap

enter image description here

Bellboy answered 19/12, 2016 at 13:50 Comment(0)
D
4

Ctrl+Shift+Enter combination (Complete Statement) also does the job and makes some more handy stuff with functions, cycles, etc.

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

Dordogne answered 5/3, 2017 at 18:25 Comment(0)
C
4

This is now available starting with IntelliJ IDEA 2021.2
check Trigger actions when saving changes
File > Settings ... > Tools > Actions on Save and select Reformat code, you can configure it to be applied to the whole file or just the changed lines.

P.S. you have to apply this to each project you have, it doesn't get applied to all projects. enter image description here

Cattail answered 26/3, 2023 at 12:43 Comment(0)
C
3

None of the solutions in Intellij is as elegant (or useful) as in Eclipse. What we need is feature request to the intellij so that we can add a hook (what actions to perform) when the IDE autosaves.

In Eclipse we can add "post-save" actions, such as organize imports and format the class. Yes you have to do a "save" or ctrl-s but the hook is very convenient.

Clifton answered 28/9, 2018 at 12:19 Comment(2)
is there any contact to intellij about htis so far? If so please post link here :)Fonteyn
@RuthiRuth found it ;) youtrack.jetbrains.net/issue/IDEABKL-5806Pardon
A
3

if you want, you can use a saveActions plugin. You can reformat file, optimized the imports and more things, it's really customizable and easy to setup.

Adermin answered 7/11, 2019 at 15:11 Comment(0)
M
2

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

Mcmaster answered 26/2, 2014 at 10:20 Comment(2)
Why the downvote? I am saying that what you want is not possible, and trying to provide with the closest possible thing..Mcmaster
The downvote is easy to explain: Mouse click. One big advantage of IntelliJ is the shortcuts and that you don't need the mouse often. If you code much, you don't want to use the mouse much, because you are faster with the keyboard. There is a shortcut for formating already.Trull
B
2

In Mac it is Alt+Command+L(assuming you haven't changed any of your modifier keys or Intellij keyboard shortcuts from it's default state)

Burgener answered 27/8, 2017 at 9:5 Comment(0)
A
2

Per the comment of Larson, this is now possible with the save-actions plugin.

Alsacelorraine answered 2/5, 2019 at 16:47 Comment(1)
Thank you but the question is about automatic reformat, not reformat on save.Charmeuse
U
2

I know this question is quite old but there is actually a Save Actions plugin for IntelliJ that can be configured to format the code automatically.

Unbated answered 29/5, 2019 at 15:31 Comment(0)
W
1

The way I implemented automatical reformating like in Microsoft Visual Studio (It doesn't work perfect):

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 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.
Weil answered 28/1, 2018 at 22:22 Comment(1)
Might be a limitation of my keyboard, but I can only seem to record 3 keys at a time.Devious

© 2022 - 2024 — McMap. All rights reserved.