Ora

How do I turn off auto format in Webstorm?

Published in WebStorm Formatting 3 mins read

To turn off auto-formatting for specific code regions in WebStorm, you can utilize special markers within your code comments after enabling this functionality in your IDE settings.

Disabling Auto-Formatting with Code Comments

WebStorm provides a convenient feature to temporarily disable its formatter for specific blocks of code. This is particularly useful when you have code that you want to keep formatted in a non-standard way, or when dealing with code from external sources that you don't want the formatter to touch.

Enabling Formatter Markers

Before you can use the comment markers, you need to enable the feature within WebStorm's settings:

  1. Open the Settings/Preferences dialog. You can do this by pressing Ctrl + Alt + S (Windows/Linux) or Cmd + , (macOS).
  2. In the left-hand menu, navigate to Editor | Code Style.
  3. Switch to the Formatter tab on the right pane.
  4. Locate and enable the option: "Turn formatter on/off with markers in code comments".
    • This setting allows WebStorm to recognize special comments that instruct the formatter to pause or resume.

Using Formatter Markers in Your Code

Once enabled, you can insert specific comments directly into your code to control the formatter:

  • To disable formatting: Type @formatter:off at the beginning of the code region you want to exclude from formatting.
  • To re-enable formatting: Type @formatter:on at the end of the region where you want the formatter to resume its normal operation.

Example:

Consider the following JavaScript code. The section between @formatter:off and @formatter:on will retain its original formatting, regardless of your WebStorm code style settings or any auto-format actions.

function someRegularCode() {
  console.log("This part will be auto-formatted.");
}

// @formatter:off
function myPreciousManualFormatting() {
  // This function's formatting is intentionally unique.
            console.log("   No auto-format for this line!");
    const value = 1;
        if (value === 1) {
            console.log("    Indentation preserved.");
        }
}
// @formatter:on

function anotherRegularCode() {
  console.log("This part will also be auto-formatted normally.");
}

Other Ways to Control Auto-Formatting

While the comment markers are ideal for specific code sections, you might also want to control auto-formatting more broadly:

  • Disable "Reformat code on save": You can prevent WebStorm from reformatting your entire file every time you save it. Go to Settings/Preferences | Tools | Actions on Save, and uncheck the "Reformat code" option. You can also configure it to only reformat "Changed lines."
  • Adjust Code Style Settings: WebStorm offers extensive customization for code styles under Editor | Code Style. You can fine-tune rules for various languages to match your preferred formatting, reducing the need to disable the formatter entirely.

By combining these methods, you gain precise control over how WebStorm formats your code, ensuring both consistency and the preservation of specific manual formatting choices.