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 below settings
    • beautify.onSave: It enables to format automatically on save after typing semi color or next line.
    • editor.formatOnSave: It enables to 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