Keith
Keith

Reputation: 1504

Disable reformatting code when saving files

I made a small change to an old java file in IntelliJ and when I save the file the IDE automatically reformats all the code. Normally this would be preferable, however it seems almost every line has unnecessary white space that gets cleaned up. Now it appears the entire file was modified even though I only made a small fix. This will make looking at the significant changes in VCS more difficult.

Is there a way to disable code reformatting when a file gets saved? So far I haven't had any luck finding the setting in the Project Settings dialog. I'm using IntelliJ IDEA 9.0.2

Alternatively I could perform the reformat, commit the changes, and then make my modification but I feel that this will end up happening a lot and I don't want to impose my formatting preferences on code owned by other groups.

Upvotes: 41

Views: 33361

Answers (7)

volmedo
volmedo

Reputation: 84

In 2023, the only thing that worked for me was going to Settings > Editor > Code Style > Formatter tab and setting the required patterns in the Do not format field.

IntelliJ Do not format setting

Upvotes: 3

Brice B
Brice B

Reputation: 183

Settings > Tools > Actions on save > first line with checkbox "Reformat code"

change "Whole file" to "Changed lines"

As it advertise only the modified lines will be affected by formatting on save.

Upvotes: 6

Himanshu Sharma
Himanshu Sharma

Reputation: 191

Preferences -> Tools -> Actions on save -> uncheck "Reformat Code"

Upvotes: 15

CrazyCoder
CrazyCoder

Reputation: 402493

Settings | Editor | General > Other | Strip trailing spaces on Save -> None.


Updated for IntelliJ Idea 2021+

Settings | Editor | General > On Save | Remove trailing spaces on: <...> -> None.

Upvotes: 46

Stepan Yakovenko
Stepan Yakovenko

Reputation: 9216

Today in 2020 IDEA is silently formatting HTML before commit, which might break your web pages appearance. To disable that unselect this checkbox:

enter image description here

Upvotes: 2

Gibolt
Gibolt

Reputation: 47297

You can add these comments to make it temporarily disable formatting on the file, or a specific function:

// @formatter:off

If you want one section to remain, you can also add this:

// @formatter:on 

IntelliJ Documentation Link

If @formatter:off is not working, you can enable it in preferences:

In IntelliJ Preferences, under Code Style, General, Formatter Control there is a checkbox "Enable formatter markers in comments"

Upvotes: 5

LemonPi
LemonPi

Reputation: 1176

For newer versions (at least WebStorm 2018), Ctrl + S is bound to a save Macro that has a reformat code action followed by save.

You can remove this reformat with Ctrl + Shift + A search for Macro then modify the save one.

Edit | Macros | Edit Macros | save | - on the Action: Reformat...

Upvotes: 2

Related Questions