84

I am using prettier to format my code in VS Code but I really dislike how it does it.

My main beef is it splitting attributes over multiple lines.

<input
    type="checkbox"
    name="asiaNews"
    id="asiaNews"
    value="asiaNews"
/>

I'd much prefer it to look like this

<input type="checkbox" name="asiaNews" id="asiaNews" value="asiaNews" />

I can't find anything in the docs or on SO

How to prevent VS Code from breaking up long HTML lines into multiple lines?

Is there a way to do it or a different tool that I can use so I can have my own custom formatting rules that suits my sensibilities?

Tristanisginger
  • 2,181
  • 4
  • 28
  • 41
  • 6
    I think it must have to do with your `printWidth` settings since it will only break your attributes into multiple lines if you're over the `printWidth` limit. You can test things out in the prettier playground: https://prettier.io/playground/ If you're over the `printWidth` I don't think there is anything you can do to prevent the attributes from going over multiple lines while using prettier. – Robert Cooper May 26 '19 at 02:42

11 Answers11

61

A quick fix is to go to Prettier Extension Settings (ctrl + shift + X) and in Prettier Extension Settings search for "Print Width" set it to 250 or anything that works for you.

1: Go to Extention Settings:

Prettier Extension Settings

2: Change the value of Print Width to your liking.

Changing the value of Print Width

To disable format code on save. Turn off the "Format On Save" and use Alt+Shift+F to format the code when ever you want.

Disable or enable Format On Save

You can visually check the setting here

Bilal Ahmad
  • 874
  • 9
  • 13
22

This happens because prettier assume that you want your code width to be in 80 characters only because of their default setting.

So You should tell prettier to I have too much space.

To do this simply create .prettierrc.json file in the root folder and add

{
 "printWidth": 600
}

And Save your file. This will clear your issue.

Aloksinghse
  • 430
  • 3
  • 5
12

After Trying A lot, I came up with below solution.

  1. Disable prettier for html files and use the default formatter provided by VS code.
  2. Set the line length as per the requirements. (I set it to 100)

Here is my settings.json looks like after above changes.

{
  "editor.formatOnSave": true,
  "html.format.wrapLineLength": 100,
  "prettier.disableLanguages": ["html"]
}

This will try to wrap the HTML attributes only if the length is greater than 100. The good thing is that even if the length exceeds 100, it won't wrap each attribute to new line.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Rahul Khunt
  • 643
  • 5
  • 6
12

Add this line to setting JSON to increase the maximuim line width,

  "prettier.printWidth": 160,
Kaleab7
  • 136
  • 1
  • 4
7

{"printWidth": 100} in prettierrc.json is good enough.

zhulien
  • 5,145
  • 3
  • 22
  • 36
7

my solution was to uninstall prettier !! and install ESLint & typeScript extension. it comes with a basic formatter which does exactly what I wanted. you can disable and enable them by search for formatter in settings

enter image description here

Daniel
  • 3,322
  • 5
  • 30
  • 40
  • 3
    After trying everyting and being annoyed for literally months, this saved me! Thanks for posting this! :D – user230910 Aug 26 '21 at 08:10
5

add new rule in .prettierrc.json that located in your project:

{
    printWidth": 160,
}
ofir_aghai
  • 3,017
  • 1
  • 37
  • 43
4

You need to uncheck the box for "Prettier: Single Attribute Per Line"

Prettier: Single Attribute Per Line

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • 2
    I doubt this was the issue as [this feature was added in 2022](https://prettier.io/blog/2022/03/16/2.6.0.html) and this question is from 2019 and it is not enabled by default. However this is useful to know if you want the behavior that the OP does not. Note, however, that setting this value to false won't keep every attribute on one line (that's still controlled by the lineWidth property), but setting it to true will force every attribute onto a separate line. – Henry Woody Oct 28 '22 at 06:59
  • This totally did not work. – Lewis Nakao Nov 18 '22 at 21:05
  • 1
    @lewdev it did for me – 5idneyD Jan 08 '23 at 12:21
  • This won't work!!! unchecking "Single Attributes Per Line" will only let them in one line IF line length <= `printWidth`!!!! – Andre Figueiredo Aug 30 '23 at 20:28
2

I tried the answers posted before and had no success.

Finally I found a solution for me by adding...

"html.format.wrapLineLength": 0

...to the setting.json file.

Shaka OS
  • 31
  • 2
1

Add

"singleAttributePerLine": true
Arty.Simon
  • 844
  • 2
  • 7
  • 21
Eugene Butkov
  • 29
  • 1
  • 1
-1

all the answer is incorrect including the marked correct answer. the word wrap is not the problem. the problem is this line in your .vscode/settings.json or maybe global settings.json

"prettier.singleAttributePerLine": true,

This line will only allow one attribute in one line. That is why many people put 250 length wrap still not fixing the problem. simply change it to false and your jsx html will not break.

  • There is already an answer that suggests this. – Henry Woody Oct 28 '22 at 06:47
  • This is so wrong!! And It's ironic suggesting this while saying everyone else is wrong. This is NOT why this option is for! Uncheck that while leaving line length <= printWidth won't solve anything! – Andre Figueiredo Aug 30 '23 at 20:34