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

In xml files use:

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

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


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