Intellij reformat on file save

Viewed 239639

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)

14 Answers

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.

If you have InteliJ Idea Community 2018.2 and above the steps are as fallows:

  1. In the top menu you click: Edit > Macros > Start Macro Recordings (you'll see a window lower right corner of your screen confirming that macros are being recorded)
  2. In the top menu you click: Code > Reformat Code (you'll see the option being selected in the lower right corner)
  3. In the top menu you click: Code > Optimize Imports (you'll see the option being selected in the lower right corner)
  4. In the top menu you click: File > Save All
  5. In the top menu you click: Edit > Macros > Stop Macro Recording
  6. You name the macro: "Format Code, Organize Imports, Save"
  7. In the top menu you clock: File > Settings. In the settings windows you click Keymap
  8. In the search box on the right you search "save". You'll find Save All (Ctrl+S). Right click on it and select "Remove Ctrl+S"
  9. Remove your search text from the box, press on the Collapse All button (Second button from the top left)
  10. Go to macros, press on the arrow to expand your macros, find your saved macro and right click on it. Select Add Keyboard Shortcut, and press Ctrl+S and okay.

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.

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.

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. Settings

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.

For PyCharm/IntelliJ IDEA:

  1. Install black.

$ pip install black

  1. Locate your black installation folder.

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.

  1. Open External tools in PyCharm/IntelliJ IDEA

On macOS: PyCharm -> Preferences -> Tools -> External Tools

On Windows / Linux / BSD: File -> Settings -> Tools -> External Tools

  1. Click the + icon to add a new external tool with the following values:
Name: Black
Description: Black is the uncompromising Python code formatter.
Program: <install_location_from_step_2>
Arguments: "$FilePath$"
  1. Format the currently opened file by selecting Tools -> External Tools -> black.

Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Black.

  1. Optionally, run Black on every file save:

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

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.

Since version 2020.1, you can activate Run on save for files directly in the Preferences of the Prettier plugin:

Preferences for Prettier plugin

Related