424

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?

Community
  • 1
  • 1
Greg Mattes
  • 33,090
  • 15
  • 73
  • 105

5 Answers5

755

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

Minh Nghĩa
  • 854
  • 1
  • 11
  • 28
Carlos Fonseca
  • 7,881
  • 1
  • 17
  • 13
  • 2
    Currently, `@formatter:on` (or whatever you define) does nothing unless `@formatter:off` was already present. There are likely other ways of turning off reformatting -- or getting the editor to not reformat your code automatically. Search your preferences. This setting is to control sections that shouldn't be reformatted when manually performing a "Reformat", for instance. – Louis St-Amour Jul 14 '14 at 19:29
  • 107
    You must go to `Preferences`->`Code Style`->`General`->`Formatter Control` and check `Enable formatter markers in comments` for this to work. – Heath Borders Aug 27 '14 at 21:06
  • 14
    In 14, this is in Preferences > Editor > Code Style > Formatter Control – Emerson Farrugia May 23 '15 at 19:29
  • 4
    In the 2016.x version it's found in Settings > Editor > Code Style. – Philippe Oct 12 '16 at 18:15
  • Another thing is that you actually can amend the markers. Then if you want to use above example your markers must match. – LazerBanana Oct 19 '16 at 08:53
  • 16
    Why does this option disabled by default? It seems very useful to put markers in the code and restrict formatting for the whole team. – Tymur Yarosh May 18 '17 at 11:03
  • one thing to note here: for html, I've seen the @formatter notation seem to have no effect when deep inside tags or template blocks – MinhajulAnwar Oct 18 '17 at 07:05
  • Note that `// @formatter: off` will not work (only without white-space) – monami Feb 02 '18 at 14:27
  • 2
    @TimurYarosh there is a ticket for it in the Intellij IDEA youtrack, feel free to upvote it, maybe Jetbrains will finally implement it: https://youtrack.jetbrains.com/issue/IDEA-118043 – Arthur Kalimullin May 30 '19 at 09:32
  • is possible only for java format? for XML files? tnx – Furetto Nov 18 '20 at 09:45
188

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 "Enable formatter markers in comments"

Discovered via How to config intellij-idea not format some part of the code?

IDEA-56995 Disabled code formatting per region using comments

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
Louis St-Amour
  • 4,065
  • 1
  • 30
  • 28
  • 7
    I want to disable this on entire project – mjs Jul 12 '14 at 09:19
  • 4
    Search preferences for "Reformat" to see checkboxes for quite a few of the ways IntelliJ reformats code automatically: on paste, on typing a closing brace, from templates... You can really dig in if you want to change the functionality. Not that it always makes sense. Use IntelliJ's support if asking a question, or their bug tracker if making a suggestion. – Louis St-Amour Jul 14 '14 at 19:23
  • 1
    It's equally possible that enabling regular expressions, then using `.` as your formatter off instruction might work. So long as the first line in your file can be reformatted. No guarantees though. :) – Louis St-Amour Jul 14 '14 at 19:25
34

In xml files, use:

<!-- @formatter:off -->
<!-- @formatter:on -->
Pang
  • 9,564
  • 146
  • 81
  • 122
olsli
  • 831
  • 6
  • 8
9

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

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
0

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

Holger Brandl
  • 10,634
  • 3
  • 64
  • 63