Are you ever stuck with VSCode formatting files that you didn’t want it to touch? Thank god there’s a simple fix for that.
I like format on save for Python and TypeScript, but not for Markdown, where it messes up my blog posts.
VSCode lets you set editor.formatOnSave for a single language.
Prerequisites
- VSCode installed
- A formatter for the language, for example Prettier or Black
Steps to set format on save for one language
- Open the Command Palette using
CTRL + Shift + Pon Windows or⌘ + Shift + Pon Mac - Search for
Preferences: Open User Settings (JSON) - Add a block for the language you want, like this:
"editor.formatOnSave": false,
"[python]": {
"editor.formatOnSave": true
},
"[typescript]": {
"editor.formatOnSave": true
}The first line turns it off everywhere, and the blocks turn it on for each language you list.
You can also do it the other way around: turn it on everywhere and set it to false for [markdown].
To find the name of a language, open a file of that type and look at the language name in the bottom right corner. Click it to see the id.