Home > OS >  code formatter settings (prettier) - how to stop autoclosing tags
code formatter settings (prettier) - how to stop autoclosing tags

Time:01-10

my problem is that prettier in VS Code is automatically closing tags on save like that:

<meta charset="UTF-8">

to:

<meta charset="UTF-8"/>

To resolve problem i have to set: ctrl , -> type formatter -> editor:default formatter set on none.

But I still want to use prettier so how to stop auto closing tags in prettier ?

CodePudding user response:

One way to disable auto-closing tags in Prettier is to set the html-self-closing option to false.

You can set this in the settings.json file in VS Code by adding the following line:

"prettier.htmlSelfClosing": false

You can also set this by adding the following configuration to your .prettierrc file:

{
  "htmlSelfClosing": false
}

Once this configuration is set, prettier will no longer automatically self-closing tags. Alternatively you can add // prettier-ignore comment on the specific line where you want prettier to ignore the auto closing tags.

For this to work, you should have the latest version of Prettier installed and configured to work with VS Code.

CodePudding user response:

In Prettier, you can configure a variety of settings to control how your code is formatted. To prevent Prettier from automatically closing tags, you can set the htmlWhitespaceSensitivity option to "strict". This will make Prettier preserve the whitespace in your HTML, which should prevent it from automatically closing tags.

You can set the option by including the following in your .prettierrc file:

{
    "htmlWhitespaceSensitivity": "strict"
}

Alternatively, you can include this configuration in your package.json file

{
"prettier": {
  "htmlWhitespaceSensitivity": "strict"
}
}

If you're using VS Code, you can also add the setting to your workspace settings, which will override the global settings:

{
"prettier.htmlWhitespaceSensitivity": "strict"
}

It's important to note that this option has been introduced in the version 2.0 of prettier and this will prevent the auto closing tags but also your html files won't be formatted as prettier doe. You will have to use other formatter tool or configure prettier to formatt html as well

  • Related