How to Exclude Specific File Types from Being Formatted on Save in VS Code

How to Exclude Specific File Types from Being Formatted on Save in VS Code

ยท

3 min read

VS Code has a handy feature that allows you to automatically format your code when you save a file. This can help keep your code clean and consistent. However, there may be times when you want to exclude specific file types from being formatted on save. In this article, we'll show you how to do just that!

Step 1: Open the settings.json file ๐Ÿ”

The first step is to open the settings.json file in VS Code. This is where you can configure your settings for the editor.

To open the settings.json file, follow these steps:

  1. Open the Command Palette by pressing Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS).

  2. Type Preferences: Open Settings (JSON) and press Enter.

This will open the settings.json file in the editor.

Step 2: Add the exclusion rule ๐Ÿ“

Next, you'll need to add a rule to the settings.json file to exclude specific file types from being formatted on save.

Here's an example of how to exclude .mdx files from being formatted on save:

"[mdx]": {
    "editor.formatOnSave": false
}

You can add this rule anywhere within the outermost curly braces {} in the settings.json file. Make sure to add a comma , after the previous entry if it's not the last entry in the file.

Here's an example of what your settings.json file might look like after adding the rule:

{
    "editor.tabSize": 2,
    "editor.wordWrap": "on",
    "[mdx]": {
        "editor.formatOnSave": false
    }
}

In this example, the rule to exclude .mdx files from being formatted on save is added after the "editor.wordWrap" setting and before the closing curly brace }.

Step 3: Save your changes ๐Ÿ’พ

After adding the rule to exclude specific file types from being formatted on save, make sure to save your changes to the settings.json file.

You can do this by pressing Ctrl+S (Windows/Linux) or Cmd+S (macOS).

That's it! Now, files with the specified extension will not be formatted on save in VS Code.

Troubleshooting ๐Ÿ”ง

If you're having trouble editing or saving the settings.json file, it could be due to a lack of permissions. You can try running VS Code as an administrator or with elevated privileges to see if that resolves the issue.

If that doesn't work, you can try manually navigating to the settings.json file on your file system and changing the permissions to allow read and write access. The location of the settings.json file varies depending on your operating system:

  • Windows: %APPDATA%\Code\User\settings.json

  • macOS: $HOME/Library/Application Support/Code/User/settings.json

  • Linux: $HOME/.config/Code/User/settings.json

To find the location of the %APPDATA% folder on Windows, you can open a File Explorer window and type %APPDATA% into the address bar and press Enter. This will take you to the AppData\Roaming folder in your user profile.

We hope this article has helped you learn how to exclude specific file types from being formatted on save in VS Code. Happy coding! ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

ย