447

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?

2
  • 1
    Official Documentation for skipping some part of the code using comments: jetbrains.com/help/idea/… Commented Jul 1, 2017 at 7:15
  • This feature should be definitively enabled by default! It would be nice Commented Dec 13, 2022 at 10:28

7 Answers 7

794

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

Sign up to request clarification or add additional context in comments.

10 Comments

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.
You must go to Preferences->Code Style->General->Formatter Control and check Enable formatter markers in comments for this to work.
In 14, this is in Preferences > Editor > Code Style > Formatter Control
In the 2016.x version it's found in Settings > Editor > Code Style.
Why does this option disabled by default? It seems very useful to put markers in the code and restrict formatting for the whole team.
|
190

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

3 Comments

I want to disable this on entire project
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.
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. :)
37

In xml files, use:

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

3 Comments

Doesn't work in xml files. Android Studio 3.4.1 based on Intelli-J.
It doesn't work in IntelliJ IntelliJ IDEA 2020.1.2 unless you enable then, in “File/Settings/Editor/Code Style“, in “Formatter Control” section.
Works in Rider.
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).

Comments

1

Personally, I use this live template for languages with C-style comments, to not having to type the markers out manually:

// @formatter:off
$SELECTION$
// @formatter:on

To install it, copy the code below, go to Settings -> Editor -> Live Templates, right click in the list and click Paste.

<template name="nofmt" value="// @formatter:off&#10;$SELECTION$&#10;// @formatter:on&#10;" description="Disable formatter for selection" toReformat="true" toShortenFQNames="true">
  <context>
    <option name="JAVA_CODE" value="true" />
    <option name="JAVA_SCRIPT" value="true" />
    <option name="KOTLIN" value="true" />
    <option name="RUST_FILE" value="true" />
    <option name="TypeScript" value="true" />
  </context>
</template>

To use it, select some code, press Ctrl + Alt + T ("Surround selection with ...") and choose nofmt.

Comments

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

Comments

-1

A related question is how to stop IntelliJ formatting all files of a certain extension. I recently started using ktlint for my codebase and wanted IntelliJ to not interfere.

Using the ktlint IntelliJ plugin, I setup ktlint to run on any save in distract-free mode.

Under Tools -> Action on Save I modified the reformat code action to exclude Kotlin files.

Under Keymap, I modified the Reformat Code command to not use the hot key I normally use for saving (CMD-s).

Under Keymap, I modified the Save All command to have the hotkey CMD-s.

Current for IntelliJ 2023.3.3, Ktlint IntelliJ plugin 0.21.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.