How to disable code formatting for some part of the code using comments?

Intellij IdeaCode Formatting

Intellij Idea Problem Overview


I'd like to selectively disable the IntelliJ IDEA code formatter for a portion of code just like I can in Eclipse. Does IntelliJ support this feature, and if so, how do I use it?

Intellij Idea Solutions


Solution 1 - Intellij Idea

Since version 13 it's possible to wrap the code with

// @formatter:off
...
// @formatter:on 

IntelliJ IDEA v.2018+:

> File > Settings > Editor > Code Style

IntelliJ IDEA v.2016+:

> Preferences > Editor > Code Style

IntelliJ IDEA v.14+:

> Preferences > Editor > Code Style > Formatter Control

You can change the formatter control markers, as long as they're in comments.


Ensure formatter markers in comments are enabled, as shown in the following figure:

Formatter Control Preference

Solution 2 - Intellij Idea

Note for Carlos' answer of @formatter:off and @formatter:on to work, you may need to enable the preference first:

In IntelliJ Preferences, under Code Style, General, Formatter Control there is a checkbox

Discovered via https://stackoverflow.com/questions/14020200/how-to-config-intellij-idea-not-format-some-part-of-the-code

IDEA-56995 Disabled code formatting per region using comments

Solution 3 - Intellij Idea

In xml files use:

<!-- @formatter:off -->
<!-- @formatter:on -->

Solution 4 - Intellij Idea

Obsolete answer from 2010:

No, it's not possible right now, I've submitted a new issue which you can track.

As a temporary solution you can use external formatter plug-in for IDEA which can utilize Eclipse code formatter (I didn't check whether this particular option is supported).

Solution 5 - Intellij Idea

It's currently not possible to exclude entire files (like regression test data xmls) from formatting. See https://youtrack.jetbrains.com/issue/IDEA-167112

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg MattesView Question on Stackoverflow
Solution 1 - Intellij IdeaCarlos FonsecaView Answer on Stackoverflow
Solution 2 - Intellij IdeaLouis St-AmourView Answer on Stackoverflow
Solution 3 - Intellij IdeaolsliView Answer on Stackoverflow
Solution 4 - Intellij IdeaCrazyCoderView Answer on Stackoverflow
Solution 5 - Intellij IdeaHolger BrandlView Answer on Stackoverflow