In Visual Studio Code, how do you automatically format your source code when the file is saved?
Enable "Format On Save" by setting
"editor.formatOnSave": true
And since version 1.49.0 editor.formatOnSaveMode
has the option modifications
that will just format the code you modified. Great when you change someone else code.
You can also set it just for one specific language:
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.formatOnSave": true #
},
Since version 1.6.1, Vscode supports "Format On Save". It will automatically use a relevant installed formatter extension to format the whole document.
Go to settings -> settings.json file and paste below code
{
"explorer.confirmDelete": false,
"editor.formatOnSave": true,
"terminal.integrated.rendererType": "dom"
}
It will format your code on save.
Below are the steps to change the VS Code auto format on save settings:
There are also Keyboard Shortcuts for formatting in VS Code. For instance, the default to format selected code should be [Ctrl]+K [Ctrl]+F (type both hotkeys in succession).
Below are the steps to change the auto format hotkey settings: