Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set tab width to 4 in prettier

I installed prettier extension for vscode and configured tab width as 4 spaces but it still indents new lines with 2 spaces. Anything I am doing wrong?

enter image description here

Here is the gif showing when I format the file:

enter image description here

EDIT: Contents of .prettierrc file:

{
  "trailingComma": "none",
  "overrides": [
    {
      "files": "**/lwc/**/*.html",
      "options": { "parser": "lwc" }
    },
    {
      "files": "*.{cmp,page,component}",
      "options": { "parser": "html" }
    }
  ]
}
like image 948
javanoob Avatar asked Oct 27 '19 22:10

javanoob


4 Answers

First, remove any .prettierrc from the working directory. Because it overrides user settings and uses the default values.

Second, set Prettier: Tab Width to 4.

Third, Unchecked Prettier: Use Tabs

And if you have to use .prettierrc file then specify all the options.

{     "singleQuote": true,     "printWidth": 80,     "editor.formatOnSave": true,     "proseWrap": "always",     "tabWidth": 4,     "requireConfig": false,     "useTabs": false,     "trailingComma": "none",     "bracketSpacing": true,     "jsxBracketSameLine": false,     "semi": true } 
like image 146
Mahbub Ul Islam Avatar answered Sep 18 '22 16:09

Mahbub Ul Islam


I think it was because, tabWidth:4 was specified in user's settings, while in workspace setting nothing was specified, so it uses default value for workspace i.e. tabWidth:2

as mentioned in comments also adding tabWidth:2 in .prettier.js would fix it!

Check out about tabWidth option in documentation

like image 33
Akshay Vijay Jain Avatar answered Sep 18 '22 16:09

Akshay Vijay Jain


Disable "use Tabs" option. Hopefully it will start working properly.

enter image description here

like image 23
Muhammad Ahsan Avatar answered Sep 21 '22 16:09

Muhammad Ahsan


Non of these possibilities solved my VS Code case with Prettier.

But the Prettier website show me how to do it!

This section (https://prettier.io/docs/en/options.html#tab-width) sent me to .editorconfig website (https://editorconfig.org/):

I found this .editorconfig file at root folder of my project, and it had two specific configs that I changed.

Before

indent_style = space
indent_size = 2

After

 indent_style = tab
 indent_size = 4

And finally it worked out!

like image 33
PedroPK Avatar answered Sep 20 '22 16:09

PedroPK