Format a code is a beautify source code on code editor using automatically on save or manually with shortcut command.

Shortcut commands for Code formatting

  • Start Visual Studio code

  • Select a code to format

  • Please run the following commands to format the code

    • Shift + Alt +F in Windows

    • Shift + Option +F in Mac

    • Ctrl + Shift + I in Linux

These documents format the entire file.

We can also format the code using settings.json

  • Go to File > Preferences >settings window opened, Select Workspaces.
  • Open a file settings.json under .vscode folder
  • update the below settings
    • beautify.onSave: It enables format automatically on save after typing semi-color or next line.
    • editor.formatOnSave: It enables format on save
    • beautify.JSfiles: format the file types
{
    "beautify.onSave": true,

    "editor.formatOnSave": true,

    "beautify.JSfiles": [
        "js",
        "json",
        "jsbeautifyrc",
        "jshintrc",
        "ts"
    ]
}

This is an example to auto-format while saving a file.