296

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.

Jeroen
64.4k47 gold badges231 silver badges372 bronze badges
asked Jun 29, 2017 at 17:14
2
  • 1
    On Mac, use Ctrl + K Shift + S On Linux, use Ctrl + K S Commented Jan 25, 2020 at 10:36
  • TIP: If you have the JS-CSS-HTML Formatter extension installed, that will autosave independently of editor.formatOnSave Commented Dec 9, 2020 at 1:18

9 Answers 9

453

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"
}
answered Jun 29, 2017 at 17:34
Sign up to request clarification or add additional context in comments.

3 Comments

very good answer. however is it possible to apply format on save on specific python module or folder??... the workaround I opened this module in its own vscode windows.
One downside to setting, say, *.in files as plaintext ("files.associations": { "*.in": "plaintext" } is that it disables code highlighting. Is there any way to disable formatting for *.in files, but tell it to highlight as a certain language?
Note that from v1.63 on you can list multiple languages for a group of settings like "[json][jsonc]":{your json settings here}. See release notes. You can btw overwrite previous settings where the last one counts. So you can do "complex" stuff with intersecting sets.
109

If you came across this question as I did because you were redirected because of this question VSCode : disable formatting of a specific file (or extension) which says, this is a duplicate (I don't feel so, because I wanted it for a specific file) and you're looking for a "one-time" solution:

VS Code has a shortcut "now" (I don't know since when) for saving a file without formatting listed under the command workbench.action.files.saveWithoutFormatting - Default keybinding should be

CTRL + K CTRL + SHIFT + S 

(simply keep CTRL pressed and then type K + SHIFT + S).

On OS X the default keybinding is

CMD + k then press s

upe
2,1821 gold badge26 silver badges40 bronze badges
answered Aug 28, 2019 at 10:06

6 Comments

Didn't know about this. Thanks! So, I can have 'FormatOnSave' enabled, but if I still want to save a file without it getting formatted, I keep Ctrl pressed, and then press K + Shift + S Just tried it. Awesome!
on linux in jan 2020 it's Ctrl + k and then just s
Great, but the next time you do a normal save, it will anyway format the file.
Or use Cmd Shift P to get to the quick commands and type save .. then it will show the command 'Save without formatting'.
It's weird, because Ctrl+K is delete current line, so just before saving you'll delete a line...
|
26

I messed up my keyboard keys with VSCode. One alternative could be utilizing the VSCode commands to save without formating by doing CTRL+SHIFT+P and executing the enter image description here command. :)

answered Feb 24, 2021 at 11:05

1 Comment

File > Preferences > Keyboard Shortcuts > enter "save" to search, then find your keybinding for "File: save without formatting"
16

On Mac, use + K, S

On Linux, use Ctrl + K S

On Windows, use Ctrl + K Ctrl + Shift + S

To check the VS Code keyboard shortcuts: Ctrl + K, Ctrl + S (yes, almost the same as the above) and search for "save without formatting"

KABA
3703 silver badges12 bronze badges
answered Jan 25, 2020 at 10:44

Comments

13

If anyone could have faced auto-format or format on saving for .env or other setting or environment file and it has a long string that caused an error and you are looking for a solution.

Add the below in your VSCode settings.json:

{
 // other part of your config
 // ...
 "files.associations": {
 ".env": "plaintext"
 },
 "[plaintext]": {
 "editor.formatOnSave": false
 },
}
CPlus
5,10348 gold badges33 silver badges76 bronze badges
answered Aug 4, 2022 at 11:13

1 Comment

I was trying to target an md file named todo.md. I replaced your .env file name with mine and it worked! It looks like the trick is the plaintext part. I guess it should work the same with any file type and name.
7

Using language specific setting can fix the issue but you will lose the code highlighting

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

My solution was to create the file called ".prettierignore" in the root and

type the file name you want to skip the auto formating. Learn more about in https://prettier.io/docs/en/ignore.html

This way, the auto-formatting will be disabled for the specific file and also you won't lose the code-highlighting.

answered Jan 2, 2023 at 5:06

Comments

4

You can use the below settings in Vscode and use "python.formatting.autopep8Args" to specify files or some pattern to ignore files you want. Of course, assuming that you are using autopep8 to format you python files other code formatters might have other ways to configure this.

{
 "editor.formatOnSave": true,
 "editor.defaultFormatter": "esbenp.prettier-vscode",
 "python.formatting.autopep8Args": ["--exclude settings.py"],
 "[python]": {
 "editor.defaultFormatter": "ms-python.python",
 "editor.formatOnSave": true
 }
}
answered Aug 27, 2021 at 5:01

1 Comment

This answer is very specific to Python and autopep8, whereas the question was about generally excluding files from a certain language ("for example only format JavaScript but not HTML files.").
3

Another way to exclude a file extension is to set the default formatter for the language to Prettier, and then ignore that file extension using .prettierignore. This has the added advantage that you can run the prettier --write . cli command to format your whole project (or just the files in a commit with lint-staged) and ignore the same file as in vscode.

// settings.json
{
 "editor.formatOnSave": true,
 "[html]": {
 "editor.defaultFormatter": "esbenp.prettier-vscode"
 },
 // OR if you want to format ALL types of documents with prettier by default
 "editor.formatOnSave": true,
 "editor.defaultFormatter": "esbenp.prettier-vscode"
}
// .prettierignore
*.html
answered Mar 24, 2023 at 14:55

Comments

0

As a workaround, this extension allow to easily toggle formatting by clicking a button on the status bar.

answered Feb 22, 2023 at 10:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.