Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 766
gabrielAnzaldo Avatar asked Jun 29 '17 17:06

gabrielAnzaldo


People also ask

How do I save code without formatting in VS Code?

TIL how to save a file on VSCode without auto formatting being applied: CMD + K S . This can be useful in situations where one is working in a repository and is not interested in having a big file auto formatted, just for the sake of a trivial change.

How do I change the format in save VS Code?

Usage. To automatically format the file on save, In Visual Studio Code, press Control + Shift + P or Command + Shift + P (Mac) to open the command palette and type setting and then select Preferences: Open User Settings option. Search for format on save setting and check the checkbox.

How do I disable all extensions in VS Code?

You will be prompted to reload VS Code after you disable an extension. If you want to quickly disable all installed extensions, there is a Disable All Installed Extensions command in the Command Palette and More Actions ( ... ) dropdown menu. Extensions remain disabled for all VS Code sessions until you re-enable them.


Video Answer


1 Answers

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" } 
like image 113
Gama11 Avatar answered Sep 21 '22 05:09

Gama11