I would like to automatically format TypeScript code using the build-in formatter when I save a file in Visual Studio Code.
I'm aware of the following options, but none of them is good enough:
Shift + Alt + F
"editor.formatOnType": true
"beautify.onSave": true
This question is related to
visual-studio-code
vscode-settings
code-formatting
For eslint:
"editor.codeActionsOnSave": { "source.fixAll.eslint": true }
No need to add commands anymore. For those who are new to Visual Studio Code and searching for an easy way to format code on saving, kindly follow the below steps.
[Cmd+,]
in Mac or using the below screenshot.You are done. Thank you.
Enter the following code in the opened settings file
{
"editor.formatOnSave": true
}
Save file
For MAC user, add this line into your Default Settings
File path is: /Users/USER_NAME/Library/Application Support/Code/User/settings.json
"tslint.autoFixOnSave": true
Sample of the file would be:
{
"window.zoomLevel": 0,
"workbench.iconTheme": "vscode-icons",
"typescript.check.tscVersion": false,
"vsicons.projectDetection.disableDetect": true,
"typescript.updateImportsOnFileMove.enabled": "always",
"eslint.autoFixOnSave": true,
"tslint.autoFixOnSave": true
}
If you would like to auto format on save just with Javascript source, add this one into Users Setting
(press Cmd, or Ctrl,):
"[javascript]": { "editor.formatOnSave": true }
Source: Stackoverflow.com