How to reformat a Vue.js component in VS Code?
Asked Answered
V

3

12

I'm using Visual Studio Code to code a Vue.js component and need to reformat the code of that component.

I did not find any built-in formatters, and the first choice for a plugin was vue-buetify which informs after installation that

There are many bugs in the extension, please do not use it, the better choice is vetur

I then tried Vetur by installing it but there is no place where I see an option to beautify the code currently in the editor. The Shift + Alt + F command has no effect.

How can I actually beautify (reformat) the code for a Vue component?

Valdez answered 16/5, 2018 at 10:43 Comment(0)
F
5

In 2022, the situation regarding formatting Vue files regaled in another answer has vastly improved.

Vetur is still the de facto solution for managing .vue files in VS Code, but in the time since this question was asked, Prettier added full support for them (and HTML proper). This means you can format them using Prettier without Vetur if you so desire.

Note that Vetur does not support VS Code's "Format Selection" functionality, even though Prettier does (for a small set of languages):

Vetur only has a "whole document formatter" and cannot format arbitrary ranges. As a result, only the Format Document command is available. The Format Selection command does not work.

If you do decide to use Vetur, you shouldn't need any other extensions to get formatting to work, as the extension comes bundled with all of its available formatters. As long as you have the extension installed and enabled, formatting with the "Format Document" command or Shift + Alt + F should work out of the box.

Vetur's settings allow the user to configure which of its available formatters is used for each language it supports (Prettier is the default for all but Sass and Stylus). The formatters can also be toggled off per language, or entirely, if you prefer to use some other formatting solution instead.

Flowery answered 20/5, 2022 at 22:20 Comment(0)
K
8

I've been fiddling with formatting quite a bit since my previously working project stopped formatting one day. Here's what I think the current state of the art is:

Use extensions vetur and prettier (specifically, esbenp.prettier-vscode Prettier - Code formatter). (You get these preinstalled by Vue.js Extension Pack esbenp.prettier-vscodeand others.)

Vetur is the (current) mandatory default tooling for vue. Accept no substitutes.

Prettier doesn't support .vue files per se, so that filetype is disabled by default: https://github.com/prettier/prettier-vscode/issues/338.

But Vetur understand its limitations and instead delegates formatting of individual sections of the .vue file to a potentially different formatter. By default, though, it delegates everything other than HTML sections to Prettier. https://vuejs.github.io/vetur/formatting.html. It disables formatting for HTML sections.

Vetur developers are down on js-beautify-html, although it is still apparently functional: https://vuejs.github.io/vetur/formatting.html. And they don't make an alternative recommendation at this time.

Prettier support for HTML, which would be the obvious choice if only it existed, is a long, sad story. Currently (May 2018), prettier formats HTML as JSX. Many subtleties are mentioned, but one issue that I have grasped is that JSX converts begin/empty/end tags to empty tags, e.g to . Apparently React and (I believe) Vue, do not like this, hence vetur disables Prettier for HTML.

So I'm going forward with enabling js-beautify-html in vetur settings, hoping for the best and keeping my eyes peeled. But I'm such a superficial coder that I may never trip over its known issues.

Kapp answered 19/5, 2018 at 1:13 Comment(0)
P
5

If you have installed the Vetur extension on VS Code,

  1. Go to the VS Code extension area.
  2. Find Vetur and select the gear icon to enter settings of Vetur.
  3. Scroll down until you find
Vetur › Format › Default Formatter: JS
Default formatter for <script> region
  1. Select pretter-eslint from dropdown menu
    (if you don't see that option you can install Prettier extension).
  2. Now you can see it formats your code automatically whenever you save.
Patmore answered 17/2, 2021 at 14:23 Comment(0)
F
5

In 2022, the situation regarding formatting Vue files regaled in another answer has vastly improved.

Vetur is still the de facto solution for managing .vue files in VS Code, but in the time since this question was asked, Prettier added full support for them (and HTML proper). This means you can format them using Prettier without Vetur if you so desire.

Note that Vetur does not support VS Code's "Format Selection" functionality, even though Prettier does (for a small set of languages):

Vetur only has a "whole document formatter" and cannot format arbitrary ranges. As a result, only the Format Document command is available. The Format Selection command does not work.

If you do decide to use Vetur, you shouldn't need any other extensions to get formatting to work, as the extension comes bundled with all of its available formatters. As long as you have the extension installed and enabled, formatting with the "Format Document" command or Shift + Alt + F should work out of the box.

Vetur's settings allow the user to configure which of its available formatters is used for each language it supports (Prettier is the default for all but Sass and Stylus). The formatters can also be toggled off per language, or entirely, if you prefer to use some other formatting solution instead.

Flowery answered 20/5, 2022 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.