Reputation: 230008
I remember seeing in either IntelliJ or Eclipse the setting to reformat (cleanup) files whenever they are saved. How do I find it (didn't find it in the settings)
Upvotes: 432
Views: 265864
Reputation: 1321
This feature is supported in latest intellij.
Upvotes: 1
Reputation: 7495
Rejoice! In IDEA 2021.2 there is finally "File
->Settings
->Tools
->Actions on Save
" where you can select Reformat code
, Optimize imports
, Rearrange code
, Run code cleanup
, Run eslint --fix
etc.
Upvotes: 42
Reputation: 1540
If you have InteliJ Idea Community 2018.2 and above the steps are as fallows:
Restart your IDE and try it.
I know what you're going to say, the guys before me wrote the same thing. But I got confused using the steps above this post, and I wanted to write a dumb down version for people who have the latest version of the IDE.
Upvotes: 30
Reputation: 645
For PyCharm/IntelliJ IDEA:
$ pip install black
On macOS / Linux / BSD:
$ which black
/usr/local/bin/black # possible location
On Windows:
$ where black
%LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
Note that if you are using a virtual environment detected by PyCharm, this is an unneeded step. In this case the path to black is $PyInterpreterDirectory$/black
.
On macOS: PyCharm -> Preferences -> Tools -> External Tools
On Windows / Linux / BSD: File -> Settings -> Tools -> External Tools
Name: Black
Description: Black is the uncompromising Python code formatter.
Program: <install_location_from_step_2>
Arguments: "$FilePath$"
Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Black.
Make sure you have the File Watchers plugin installed.
Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:
Name: Black
File type: Python
Scope: Project Files
Program: <install_location_from_step_2>
Arguments: $FilePath$
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$
Uncheck “Auto-save edited files to trigger the watcher” in Advanced Options
To format Python files with Black, I followed this guide, which also uses File Watcher: https://black.readthedocs.io/en/stable/editor_integration.html
Upvotes: 6
Reputation: 60357
Since version 2020.1, you can activate Run on save for files directly in the Preferences of the Prettier plugin:
Upvotes: 3
Reputation: 3475
If it's about Prettier, just use a File Watcher :
references => Tools => File Watchers => click + to add a new watcher => Prettier
https://prettier.io/docs/en/webstorm.html#running-prettier-on-save-using-file-watcher
Upvotes: 2
Reputation: 3539
If you're developing in Flutter, there's a new experimental option as of 5/1/2018 that allows you to format code on save.
Upvotes: 16
Reputation: 4739
This solution worked better for me:
Note: You will have to check the box "Do not show this message again" the first time for the organized imports, but it works as expected after that.
step 2. with: Edit -> Macros -> "Start Macro Recording"
step 6. with: Edit -> Macros -> "Stop Macro Recording"
Everything else remains the same.
8. The Preferences contain the Keymap settings. Use the input field to filter the content, as shown in the screenshot.
Upvotes: 473
Reputation: 4038
I suggest the save actions plugin. It also supports optimize imports and rearrange code.
Works well in combination with the eclipse formatter plugin.
Search and activate the plugin:
Configure it:
Edit: it seems like it the recent version of Intellij the save action plugin is triggered by the automatic Intellij save. This can be quite annoying when it hits while still editing.
This github issue of the plugin gives a hint to some possible solutions:
https://github.com/dubreuia/intellij-plugin-save-actions/issues/63
I actually tried to assign reformat to Ctrl+S and it worked fine - saving is done automatically now.
Upvotes: 336
Reputation: 129
IntellIJ 14 && 15: When you are checking in code in Commit changes dialog, tick the Reformat code checkbox, then IntelliJ will reformatting all the code that you are checking in.
Source: www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours
Upvotes: 6
Reputation: 3673
I set it to automatically clean up on check-in, which is usually good enough for me. If something is too ugly, I'll just hit the shortcut (Ctrl-Alt-L, Return). And I see they have an option for auto-formatting pasted code, although I've never used that.
Upvotes: 60
Reputation: 10385
I wound up rebinding the Reformat code...
action to Ctrl-S, replacing the default binding for Save All
.
It may sound crazy at first, but IntelliJ seems to save on virtually every action: running tests, building the project, even when closing an editor tab. I have a habit of hitting Ctrl-S pretty often, so this actually works quite well for me. It's certainly easier to type than the default bind for reformatting.
Upvotes: 10
Reputation: 951
Below is Neil's answer updated.
Upvotes: 95
Reputation: 3454
Ctrl + Alt + L is format file (includes the two below)
Ctrl + Alt + O is optimize imports
Ctrl + Alt + I will fix indentation on a particular line
I usually run Ctrl + Alt + L a few times before committing my work. I'd rather it do the cleanup/reformatting at my command instead of automatically.
Upvotes: 21
Reputation: 2718
I thought there was something like that in IntelliJ, but I can't find it. The only clean-up that happens at save is that white space at the ends of lines is removed. I thought I had to specify that behavior at one point, but I don't see anything related at this point.
Upvotes: 2