Monday, May 8, 2023

Visual Studio Code: Disable Format on Save per-File (including wildcards)

In this post, we'll explore how to disable the formatOnSave option for specific files, multiple files, using wild cards, and files with certain extensions.


Disabling formatOnSave for a specific file

To disable formatOnSave for a specific file, you can add the following setting to your settings.json file:

"[file path/filename.ext]": {
    "editor.formatOnSave": false
}

Replace file path/filename.ext (noted in boldface) with the path and filename of the file for which you want to disable formatOnSave.


Disabling formatOnSave for multiple files

To disable formatOnSave for multiple files, you can add the following setting to your settings.json file:

"editor.formatOnSave": true,
"[file path/filename1.ext]": {
    "editor.formatOnSave": false
},
"[file path/filename2.ext]": {
    "editor.formatOnSave": false
}

Replace file path/filename1.ext and file path/filename2.ext  (noted in boldface) with the path and filenames of the files for which you want to disable formatOnSave.


Disabling formatOnSave using wildcards

You can also disable formatOnSave for files that match a specific pattern using wildcards. For example, to disable formatOnSave for files that have a specific prefix, you can add the following setting to your settings.json file:

"editor.formatOnSave": true,
"[prefix]*.ext": {
    "editor.formatOnSave": false
}

Replace prefix  (noted in boldface) with the desired prefix for the files you want to exclude from formatOnSave.

Similarly, to disable formatOnSave for files that have multiple possible extensions, you can use a wildcard to match the extensions. For example:

"editor.formatOnSave": true,
"[file path/*.ext1, *.ext2]": {
    "editor.formatOnSave": false
}

Replace file path with the path to the directory containing the files you want to exclude from formatOnSave. Replace ext1 and ext2 with the extensions of the files you want to exclude from formatOnSave.

Conclusion

And that's it! With these settings, you can easily disable the formatOnSave option for specific files, multiple files, or using wildcards.

No comments :

Post a Comment