How do you format code on save in VS Code

290,965

Solution 1

As of September 2016 (VSCode 1.6), this is now officially supported.

Add the following to your settings.json file:

"editor.formatOnSave": true

Solution 2

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.

  1. Open Settings by pressing [Cmd+,] in Mac or using the below screenshot.

VS Code - Open Settings Command Image

  1. Type 'format' in the search box and enable the option 'Format On Save'.

enter image description here

You are done. Thank you.

Solution 3

If you would like to auto format on save just with Javascript source, add this one into Users Setting (press CmdShiftP or CtrlShiftP then type Open Settings (JSON) to open settings.json file)

"[javascript]": { "editor.formatOnSave": true }

Solution 4

For eslint:

"editor.codeActionsOnSave": { "source.fixAll.eslint": true }

Solution 5

For neet formatting for any language you can use Prettier - code formatter. After applying this you can format code Alt + Shift + f enter image description here

Share:
290,965
Tomas Nikodym
Author by

Tomas Nikodym

Updated on July 24, 2022

Comments

  • Tomas Nikodym
    Tomas Nikodym almost 2 years

    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:

    • Format manually Shift + Alt + F
    • Format on type "editor.formatOnType": true
      • It formats the line when you press enter. Unfortunatelly, it leaves it unformatted when you mouse-click another line or press up/down arrow.
    • Use existing extension
      • I tried this one, but it does not seem to work too well.
    • Use beautify "beautify.onSave": true
      • It does not work with TypeScript
    • Write custom extension
      • It's tricky if you want to handle autosaves and builds correctly.