I’m using VSCode
for a Node.js project, and I have set up my editor to automatically sort JSON files on save, using the following setting in my settings.json
:
"editor.codeActionsOnSave": {
"source.sort.json": "always"
}
I want to exclude package.json
from being automatically sorted when I save it, while keeping the auto-sort
feature for other JSON files in my project. Is there a way to specifically exclude package.json
from the editor.codeActionsOnSave
rule, or any other workaround that would allow me to achieve this behavior?
I looked into VSCode documentation
and tried searching for similar issues but couldn’t find a direct solution. Any suggestions or alternative approaches are greatly appreciated.
2
Answers
OK, I've found a halfway decent way to integrate without hacking.
settings.json
to ensure that it does not apply globally to all files. Here's what you should remove:package.json
asjsonc
(JSON with comments), which is not targeted by the auto-sort setting. Add this to yoursettings.json
:auto-sort
:Until a more convenient solution is implemented by the VSCode developers, you can use this solution here.
Every JSON will be sorted and every JSONC not. You can extend this list below:
With this setup,
package.json
remains untouched by the auto-sort feature, while files likesettings.json
continue to be sorted. This distinction hinges on the file extension —json
for sorting,jsonc
for no sorting.Be aware of the last letter
c
Another Example
To prevent a file from being formatted on save, you can use the corresponding shortcuts:
You can check your keybindings in Settings > Keyboard Shortcuts.