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
The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.
TIL how to save a file on VSCode without auto formatting being applied: CMD + K S . This can be useful in situations where one is working in a repository and is not interested in having a big file auto formatted, just for the sake of a trivial change.
You can also configure this to run automatically when saving a file. To do this, go to File->Preferences->Settings: Under Text Editor, select Format On Save. Now when saving a file, it will be beautified.
As of September 2016 (VSCode 1.6), this is now officially supported.
Add the following to your settings.json
file:
"editor.formatOnSave": 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 [Ctrl+,]
in Windows/Linux) or using the below screenshot.You are done. Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With