The dialog appears when you press Ctrl+Alt+Shift+L in the editor of the current file. 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.
The formatting shortcuts in Intellij IDEA are :
I have found two ways to do this:
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With