乐闻世界logo
搜索文章和话题

How to exclude file extensions and languages from "format on save" in VSCode?

1个答案

1

In VSCode, if you want to exclude specific file extensions or languages from "Format on Save", you can achieve this by modifying the settings.json file. Below, I will explain how to do this in detail and provide a specific example.

Step 1: Open the Settings File

First, to modify the settings, you need to open the VSCode settings file settings.json. This can be done by following these steps:

  1. Open VSCode.
  2. Press Ctrl + , or click the gear icon in the bottom-left corner and select Settings.
  3. In the search bar, type settings.json.
  4. Click the Edit in settings.json link that appears in the results.

Step 2: Modify Settings

In the settings.json file, you can add or modify settings to exclude specific file extensions or languages. There are two key settings:

  1. files.exclude: Used to exclude specific files or folders so they don't appear in Explorer or other places.
  2. [language]: Settings for specific languages, where you can add configurations such as disabling formatting.

Exclude File Extensions

If you want to prevent automatic formatting for specific file extensions on save, such as .min.js, you can set it as follows:

json
"editor.formatOnSave": true, "[javascript]": { "editor.formatOnSave": true }, "**/*.min.js": { "editor.formatOnSave": false }

In this example, all JavaScript files are formatted on save by default, but files ending with .min.js are not formatted on save.

Exclude Specific Language

If you want to disable automatic formatting on save for a specific programming language, such as Python, you can set it as follows:

json
"editor.formatOnSave": true, "[python]": { "editor.formatOnSave": false }

In this configuration, although global "Format on Save" is enabled, it is disabled for Python files.

Step 3: Save and Test

After setting this up, save the settings.json file and try modifying the corresponding file types to verify if the settings work. If the settings are correct, saving specific files or writing code in specific languages will not trigger automatic formatting.

2024年8月10日 08:35 回复

你的答案