W3docs

Format code command for PHP/HTML in Visual Studio Code

To format code in Visual Studio Code, you can use the following steps:

To format code in Visual Studio Code, you can use the following steps:

  1. Ensure a formatter extension is installed for your language (e.g., "PHP Intelephense" or "PHP CS Fixer" for PHP, or "Prettier" for HTML).
  2. Set the default formatter in VS Code settings:
    • Open Settings (Ctrl+, on Windows/Linux, Cmd+, on macOS).
    • Search for editor.defaultFormatter.
    • Set it to your installed extension for php or html.
  3. Select the text you want to format, or press Ctrl+A (Cmd+A on Mac) to select all.
  4. Press Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS) to format the document immediately.

Alternatively, you can right-click the selected code and choose "Format Document" or "Format Selection". This will apply the default formatter settings for the current language without requiring additional prompts.