Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

visual studio code - How to exclude file extensions and languages from "format on save" in VSCode?

Currently in VSCode settings you can configure format on save as following:

"editor.formatOnSave": true

I want to exclude some file extensions, for example only format JavaScript but not HTML files.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use language specific settings to enable it for a specific language only, e.g. JavaScript:

"[javascript]": {
    "editor.formatOnSave": true
}

To disable it for a specific language, you could switch the global default to true and combine it with a language-specific false:

"editor.formatOnSave": true
"[javascript]": {
    "editor.formatOnSave": false
}

Note that language specific settings are based on language identifiers rather than directly on file extensions. There's an open feature request to allow for file extension specific settings as well.

In cases where the language ID isn't specific enough, "files.associations" could be used to remap files with a specific extension and/or in a specific directory to another ID, but this will affect syntax highlighting, code completion, etc. as well. For instance, this would work to disable formatting for JavaScript files in out directories, but they will be treated as plaintext:

"[javascript]": {
    "editor.formatOnSave": true
},
"files.associations": {
    "**/out/**/*.js": "plaintext"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...