Right-click in your text editing area and choose Format Document With.... A popup will appear on top then choose Choose default formatter and then choose Prettier

Answer from Ayush Gupta on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › languages › html
HTML in Visual Studio Code
November 3, 2021 - Visual Studio Code provides basic support for HTML programming out of the box. There is syntax highlighting, smart completions with IntelliSense, and customizable formatting.
🌐
Medium
thiraphat-ps-dev.medium.com › the-best-code-formatters-for-vs-code-11704e787f92
The Best Code Formatters for VS Code | by Thiraphat Phutson | Medium
June 3, 2024 - Code formatters automatically adjust ... of the best code formatters for VS Code that you should consider incorporating into your workflow. Prettier is an opinionated code formatter that supports a wide range of languages, including JavaScript, TypeScript, CSS, HTML, and ...
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
JS-CSS-HTML Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - Format ,prettify and beautify JS, CSS, HTML code by using shortcuts, context menu or CLI
🌐
freeCodeCamp
forum.freecodecamp.org › t › help-with-formatting-of-my-html-files-in-vs-code › 487585
Help with formatting of my HTML files in VS Code - The freeCodeCamp Forum
December 4, 2021 - For some reason, the paragraph content in my HTML files is breaking into multiple lines in VS Code. I made the mistake of making changes to my settings based on a video I watched. I thought I undid the changes I made, but maybe I missed something. And that is assuming the change in the editor ...
Find elsewhere
Top answer
1 of 1
6

Correct Prettier Extension


There are many prettier extensions to choose from, but only one is the official prettier extension that was written by the same people who maintain prettier. You want to make sure that you have the OFFICIAL PRETTIER EXTENSION.

  • HERE IS THE LINK TO THE OFFICIAL PRETTIER EXTENSION

  • To verify that it is the official extension, its unique extension ID will be: "esbenp.prettier-vscode".

  • The link shows the official extensions page in the Visual Studio website view, you will probably want to download it inside of your editor as opposed to installing it via a VSIX file.



  • You need to configure your default formatter in a generalized way to be able to format JavaScript & HTML. Your trying to specify configurations on a per language basis, which is usually the wrong thing to do if your trying to configure a formatter to format more than one language or file type. You just want prettier to be set as the default formatter, as opposed to the default formatter for HTML, there is a big difference between the two. One formats all languages, the other formats HTML only. for all of VS Code.

  • We also want to make sure that prettier is the formatter we are equipping. Currently you don't have the correct formatter configured. Your trying to use VS Codes Languag-features formatter to get the Prettier formatter to work.


I have included two examples of configuring your default formatter below.
  1. The first example shows the improper configuration that you are currently using. Which configures the formatter specific to a file extension, and it is configuring the wrong formatter

  2. The second configuration is the CORRECT CONFIGURATION TO USE. It configures the Official Prettier Formatting Extension for all of VSCode.



#1

The example below shows what NOT to do!
    /** @file "settings.json" */

    {
        "[html]": {
           "editor.defaultFormatter": "vscode.html-language-features"
    }

The incorrect configuration above does two things wrong.

  1. "vscode.html-language-features" is not the correct extension ID. You need to use the prettier formatter's extension ID. The Prettier extension id is as follows "esbenp.prettier-vscode"

  2. Your configuring the formatter to work for HTML files only.

    • "[HTML]":{ // settings ... } <-- adding prettier to that block configures prettier to only work if a file extension ends in HTML. The problem is, HTML doesn't always get placed in an .html document.

For the context of this question, it isn't technically wrong, but its not the best configuration to use when trying to troubleshoot the issue, instead; you should just set your default formatter as Prettier across all languages. Then once you get Prettier working, you can use settings that specify the languages you want to use it for, and languages you don't want to use it for.



#2

This snippet shows the CORRECT configuration to use
  /** @file "settings.json" */

  {
      "editor.defaultFormatter": "esbenp.prettier-vscode",
      "editor.formatOnSave": true,
  }

Notice that I added format on save? You want that so when you press CTRL + S your code is formatted.



Another thing you don't want to do is configure prettier in your settings.json file.

DONT DO THIS

/** @file "settings.json" */

{
    "prettier.arrowParens": "avoid",
    "prettier.jsxBracketSameLine": true,
    "prettier.bracketSpacing": true,
    "prettier.embeddedLanguageFormatting": "auto",
    "prettier.htmlWhitespaceSensitivity": "css",
    "prettier.insertPragma": false,
    "prettier.jsxSingleQuote": true,
    "prettier.printWidth": 100,
    "prettier.proseWrap": "preserve",
    "prettier.quoteProps": "as-needed",
    "prettier.requirePragma": false,
    "prettier.semi": true,
    "prettier.singleQuote": false,
    "prettier.tabWidth": 2,
    "prettier.trailingComma": "es5",
    "prettier.useTabs": true,
    "prettier.vueIndentScriptAndStyle": false,
}

WHAT YOU WANT TO DO TO CONFIGURE PRETTIER, IS ADD A .prettierrc CONFIGURATION FILE TO EVERY PROJECT.


  1. The prettier configuration file is named ".prettierrc"

  2. The .prettierrc file should be placed in the ROOT directory of your project.

  3. Add the following to your ".prettierrc" document.

{
  "trailingComma": "es5",
  "tabWidth": 4,
  "semi": false,
  "singleQuote": true
}

Here is the link to Prettier's page that shows all available configurations you can add to your prettier file.

At this point Prettier should be working

If prettier isn't working, then you are going to need to get the debugging information about why it isn't working from the prettier extension. Prettier outputs this information for you, but you have to know how to get it.



Getting Debugging Info from the V.S. Code Prettier Extension


NOTE: Stack overflow requires debugging details for every question about an issue. If prettier isn't working getting these debugging details will help you ask your question without people giving you to hard of a time on here.

Add the following setting to your settings.json file

"prettier.enableDebugLogs": true

then open your terminals panel, but instead of using the terminal, your going to click on the OUTPUT option at the top of the panel instead, then in the drop down to the right, select prettier from the menu. This will tell you what is happening each time prettier tries to format.

If your still stuck, get to this point, where you have started to debug/troubleshoot prettier, and comment below about what the debugging info says, or ask a new question and include the debugging info.



END OF EDIT

🌐
Reddit
reddit.com › r/vscode › best auto formatter extension?
r/vscode on Reddit: Best Auto Formatter Extension?
November 19, 2021 -

So I keep seeing Prettier come up when I look for an auto-formatter but I'm a little worried as it says that it's opinionated.

My workplace has very specific coding standards and I'd prefer one that is configurable as possible.

Also preferably with the ability to share those settings with other members of my team.

🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
VS Code Pretty Formatter
Extension for Visual Studio Code - VS Code extension to format your code using Pretty Diff
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Beautify - Visual Studio Marketplace
Extension for Visual Studio Code - Beautify code in place for VS Code
🌐
Reddit
reddit.com › r/vscode › can you recommend a good all-in-one beautifier for html, css, js, xml, sass, scss, & less?
r/vscode on Reddit: Can you recommend a good all-in-one Beautifier for HTML, CSS, JS, XML, SASS, SCSS, & LESS?
August 20, 2020 -

I'm using HookyQR's "Beautify" extension right now, but it doesn't work with XML or LESS. (Maybe SCSS, but I haven't tried yet.)

I could just install other extensions that support those other languages, but I'd rather use a single extension.

There are tons of beautifiers in the marketplace, but I'm hoping to take advantage of everyone's expertise here so I don't have to manually try a bunch. (Also, there are usually some good ones out there with unusual names or bad SEO that are hard to find.)

Thanks!

🌐
Alphr
alphr.com › home › how to auto-format code in vs code
How To Auto-Format Code in VS Code
June 8, 2022 - Web developers and software engineers use HTML every day and can benefit from an auto-formatting tool. If your code editor of choice is VS Code, here’s how you can find Prettier, a popular code formatter, and turn on the auto-format feature.
🌐
GitHub
github.com › mohd-akram › vscode-html-format
GitHub - mohd-akram/vscode-html-format: A Visual Studio Code extension for formatting HTML documents.
Formats HTML documents by auto-indenting, wrapping and removing unnecessary whitespace while preserving newlines.
Starred by 12 users
Forked by 3 users
Languages   TypeScript
Top answer
1 of 16
6008

The code formatting is available in Visual Studio Code through the following shortcuts:

  • On Windows Shift + Alt + F
  • On Mac Shift + Option + F
  • On Linux Ctrl + Shift + I

Alternatively, you can find the shortcut, as well as other shortcuts, through the submenu View / Command Palette, also provided in the editor with Ctrl + Shift + P (or Command + Shift + P on Mac), and then searching for format document.

For unsaved snippets

  1. Open command palette (Win: F1 or Ctrl + Shift + P)

  2. Find "Change Language Mode"

  3. Select language e.g. json. By now, the syntax should be highlighted.

  4. Format document (e.g. Open Command Palette -> "Format Document")

Unformat

  1. Select text
  2. Command Palette -> Join Lines

'Show the pics'

2 of 16
607

Code Formatting Shortcut:

Visual Studio Code on Windows - Shift + Alt + F

Visual Studio Code on MacOS - Shift + Option + F

Visual Studio Code on Ubuntu - Ctrl + Shift + I

You can also customize this shortcut using a preference setting if needed.

Code Formatting While Saving the File:

Visual Studio Code allows the user to customize the default settings.

If you want to auto format your content while saving, add the below code snippet in the work space settings of Visual Studio Code.

Menu FilePreferencesWorkspace Settings

{
  // Controls if the editor should automatically format the line after typing
  "beautify.onSave": true,

  "editor.formatOnSave": true,

  // You can auto format any files based on the file extensions type.
  "beautify.JSfiles": [
      "js",
      "json",
      "jsbeautifyrc",
      "jshintrc",
      "ts"
  ]
}

Note: now you can auto format TypeScript files. Check my update.

🌐
GitHub
github.com › squizlabs › PHP_CodeSniffer › discussions › 3941
vs code formatting mixed php and html · squizlabs/PHP_CodeSniffer · Discussion #3941
You can also specify specific settings for HTML formatting within PHP files:: "[php]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } Use Prettier or Other Formatter: Consider using a code formatter like Prettier for consistent formatting across HTML and PHP code blocks.
Author   squizlabs
🌐
CodeForGeek
codeforgeek.com › home › top 15 best visual studio code extensions for web development
Top 15 Best Visual Studio Code Extensions For Web Development | CodeForGeek
December 22, 2023 - You can install it by searching for this extension’s name in the extension section of the Visual Studio Code. Before installing ESLint in visual studio, install it as a global package first. ... This extension performs the formatting of the JavaScript, CSS, and HTML code(i.e.
🌐
Reddit
reddit.com › r/vscode › formatting html
r/vscode on Reddit: Formatting html
September 12, 2024 -

When formatting with vscode, all the html attributes are surrounded with double quotes by default

How do I change it so that only those attributes that I have quoted must be retained, but those I’ve not surrounded with quotes must remain as such ?

Reason : we have an internal framework which takes non string arguments for passing props down to child component (sort of like react). But when I pass such props, they always get formatted as string. No ones bothered to write a custom formatted or something for this(I should be possible to write one right?)

Edit: added reason