241

When I execute a Format Document command on a Vue Component.vue file VSCode replace all single quoted string with double quoted string.

In my specific case this rule conflicts with electron-vue lint configuration that require singlequote.

I don't have prettier extensions installed (no prettier.singleQuote in my setting)

How to customize VSCode to avoid this?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
attdona
  • 17,196
  • 7
  • 49
  • 60
  • 3
    Possible duplicate of [VS Code Auto Indent / Code Formatting changes single quotation marks to double](https://stackoverflow.com/questions/47091719/vs-code-auto-indent-code-formatting-changes-single-quotation-marks-to-double) – PatrickSteele Feb 19 '18 at 11:46
  • Related - https://stackoverflow.com/a/64749368/984471 – Manohar Reddy Poreddy Nov 09 '20 at 10:00
  • Also related: [Visual Studio Code - Adjust import quotation setting](https://stackoverflow.com/questions/42952453/visual-studio-code-adjust-import-quotation-setting) – Liam Aug 27 '21 at 08:01
  • After reading through the answers here (including the accepted one) what actually worked for me was creating a .prettierrc.json and adding the { "singleQuote": true, "jsxSingleQuote": true } to it. I already had these settings in the user settings in VSCode but they had no effect. Hope this helps someone! – Ed Bishop Aug 08 '22 at 15:22

32 Answers32

256

I dont have prettier extension installed, but after reading the possible duplicate answer I've added from scratch in my User Setting (UserSetting.json, Ctrl+, shortcut):

"prettier.singleQuote": true

A part a green warning (Unknown configuration setting) the single quotes are no more replaced.

I suspect that the prettier extension is not visible but is embedded inside the Vetur extension.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
attdona
  • 17,196
  • 7
  • 49
  • 60
  • 2
    This didn't work for me. I had to use `vetur.format.defaultFormatterOptions` instead. See [https://vuejs.github.io/vetur/formatting.html#settings](https://vuejs.github.io/vetur/formatting.html#settings). – user1620220 Jan 17 '19 at 14:58
  • After opening the quick search, type ">user settings" and click "Preferences: Open User Settings". In the search box of your preferences, type "prettier.singleQuote" and click the checkbox for prettier single quote. – Kody Jul 26 '19 at 16:22
  • 34
    This didn't work for me, `quote_type = single` in `[*.myDesiredFileExtension]` section within `.editorconfig` file, was the solution. –  Jan 09 '20 at 08:44
  • 1
    and you my friend are a super hero – rotimi-best Feb 09 '21 at 08:05
186

Well, like the guy (@user2982122) mentioned but instead of File go to Code -> Preferences -> Settings, then look for Quote, select Prettier and check both boxes

enter image description here enter image description here

mustapha mekhatria
  • 3,495
  • 1
  • 20
  • 26
133

For projects that use .editorconfig file by default. The formatter will ignore the rules in the settings and use the rules in .editorconfig, then you can either:

  • Remove .editorconfig file, and use your VSCode settings.
  • Add quote_type = single to the .editorconfig file regarding your file type. You can also set quote_type value to double or auto.
Tan Nguyen
  • 1,636
  • 1
  • 12
  • 9
88

It looks like it is a bug open for this issue: Prettier Bug

None of above solution worked for me. The only thing that worked was, adding this line of code in package.json:

"prettier": {
    "singleQuote": true
  },
Avjol Sakaj
  • 1,281
  • 10
  • 14
73

At the time of writing (June 2022):

please consider that .editorconfig overwrites every other configuration at the end, find the file (most probably on the root of your project), edit it and add the following:

[*]
quote_type = single
  • 3
    This is the right one! – Alex Aug 10 '22 at 06:25
  • Open the **Output** tab, if this message shows: `Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used`, then this answer is the solution. – testing_22 Feb 22 '23 at 16:07
  • FYI, `quote_type` is a proposal that is not intended to be implemented by EditorConfig. See [here](https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#ideas-for-domain-specific-properties). – N'Bayramberdiyev Jul 12 '23 at 07:25
45

From the vuejs/vetur issue page https://github.com/vuejs/vetur/issues/986# This solution worked for me.

In VSCodes settings.json file add this entry

"vetur.format.defaultFormatterOptions": {
    "prettier": {
        "singleQuote": true
    }
},
Dave Pile
  • 5,559
  • 3
  • 34
  • 49
33

Install prettier extension and paste below code in your VSCode settings.json file

 "prettier.useEditorConfig": false,
 "prettier.singleQuote": true

this will ignore your .editorconfig file setting.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
19

What worked for me was setting up the .prettierrc.json config file. Put it to the root of your project with a sample config like this:

{
  "singleQuote": true,
  "trailingComma": "all",
  "tabWidth": 2,
  "semi": true,
  "arrowParens": "always"
}

After triggering the Format Document command, all works just as expected.

Side note: What comes as a bonus with this solution is that each team member gets the same formatting outputs thanks to the present config file.

marson
  • 913
  • 10
  • 32
15

Correct solution :

I add .prettierrc.js file in my main root project and write

module.exports = {
    singleQuote: true
  };
Omar Hasan
  • 719
  • 7
  • 18
  • I tried this but it throws an error: "ESLint: Failed to load config "defaults/configurations/eslint" to extend from." Best would be to add this in package.json. [Solution](https://stackoverflow.com/a/59137494/9294131) – Avjol Sakaj Dec 02 '19 at 11:01
  • did you put your .prettierrc.js file in the root of the project ? – Omar Hasan Dec 04 '19 at 09:09
14

For newbies like me:

From the menu Nav bar at the top: Select File -> Preferences -> Settings. In the search text box, type in Quote In the filtered list that appears below, look for the gear icon and next to it - "Prettier". Click on check box to enable "Prettier: Single Quote"

user2982122
  • 161
  • 1
  • 5
10

Try one of these solutions

  1. In vscode settings.json file add this entry "prettier.singleQuote": true
  2. In vscode if you have .editorconfig file, add this line under the root [*] symbol quote_type = single
  3. In vscode if you have .prettierrc file, add this line
{
    "singleQuote": true,
    "vetur.format.defaultFormatterOptions": {
        "prettier": {
            "singleQuote": true
        }
    }
}
javidabd
  • 354
  • 4
  • 10
  • 2
    second option worked, unfortunately the other answers in this page didn't work – Mo. Feb 21 '20 at 12:51
10
quote_type = single

add this inside .editorconfig

# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
quote_type = single
Max
  • 393
  • 4
  • 4
9

I had the same issue in vscode. Just create a .prettierrc file in your root directory and add the following json. For single quotes add:

{
  "singleQuote": true
}

For double quotes add:

  {
      "singleQuote": false
  }
Hamfri
  • 1,979
  • 24
  • 28
9

After struggling with the issue I found a useful tool. If you click on the Prettier word in the right lower corner you will get the Output window opened. In that window once you run formatting (in my case it is Alt + Shift + F) you will see all the configurations which prettier will use to format the document. So, we can clearly see that specifying the prettier in the prettier.singleQuote is wrong. It should just be singleQuote. Hence, having the .prettierrc file in my user root folder with the following contents produced the desired result:

{
    "trailingComma": "none",
    "useEditorConfig": false,
    "singleQuote": true
}

Also, make sure that you have the Prettier extension installed.

enter image description here enter image description here

manymanymore
  • 2,251
  • 3
  • 26
  • 48
9

First, install the Prettier extension. Create a .prettierrc configuration file at the root of your project. And add config like below:

{
  "trailingComma": "es5",
  "singleQuote": true,
  "jsxSingleQuote": true,
  "printWidth": 100,
  "tabWidth": 2,
  "semi": true,
  "endOfLine": "auto"
}
RS Shonjoy
  • 245
  • 3
  • 2
8

As noted by @attdona the Vetur extension includes prettier.

While you can change the prettier settings, as per the accepted answer, you can also change the formatter for specific regions of a vue component.

Here, for example, I've set Vetur to use the vscode-typescript formatter as it uses single quotes by default:

vscode vetur settings

Richard Banks
  • 12,456
  • 3
  • 46
  • 62
8

in .prettierrc add

{
  "arrowParens": "avoid",
  "semi": false,
  "singleQuote": true
}
Sebastian
  • 1,321
  • 9
  • 21
Ricardo de Paula
  • 572
  • 5
  • 16
7

I'm using typescript, for me it got resolved with checking "Tslint integration" flag under prettier settings (in vscode preferences):

vscode settings for prettier, fixing double quote auto formatting issue

Be Kind
  • 4,712
  • 1
  • 38
  • 45
  • https://github.com/microsoft/vscode/issues/21424#issuecomment-282524286 - this is when/where it went into VSCode – Sam Nov 29 '19 at 21:37
7

There only solution that worked for me: and only for Angular Projects:

Just go into your project ".editorconfig" file and paste 'quote_type = single'. Hope it should work for you as well.

  • i needed this solution specifically for an angular project as well, all other chnages to prettier settings did not work. – J King Dec 14 '20 at 14:48
7

It works for me to check single quote in Prettier as well tslint.autoFixOnSave as true

enter image description here

enter image description here

Rudra
  • 109
  • 2
  • 7
6

I added file called .prettierrc in my project folder. File content:

{
    "singleQuote": true,
    "vetur.format.defaultFormatterOptions": {
        "prettier": {
            "singleQuote": true
        }
    }
}
0lukasz0
  • 3,155
  • 1
  • 24
  • 40
6

In my case, the problem was in the escaping \ character inside the string:

message = 'Error argument is not an object, it\'s ' + typeof error

Turning on the avoidEscape option and using double quotes for that string solved the problem:

message = "Error argument is not an object, it's " + typeof error

.eslintrc.js

module.exports = {
  rules : {
    // Other rules...
    'quotes' : ['error', 'single', {'avoidEscape' : true}],
  }
}
mcmimik
  • 1,389
  • 15
  • 32
5

You can use this in settings.json

"javascript.preferences.quoteStyle": "single"
mperk
  • 324
  • 3
  • 8
3

Use this extension.

https://marketplace.visualstudio.com/items?itemName=BriteSnow.vscode-toggle-quotes

cmd ' (ctrl ' on win/Linux) will cycle among ' " `

U.A
  • 2,991
  • 3
  • 24
  • 36
3

For JSX use:

{"jsxSingleQuote": false}
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Juan Angel
  • 656
  • 6
  • 6
1

Well for me both options solved the issue:

  1. By adding inside the .prettierrc - "singleQuote": true

  2. Or by adding following inside the package.json -> "prettier": { "singleQuote": true }

Though I tried also adding .prettierrc.js and have following

module.exports = { singleQuote: true };

This didn't worked.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
1

This works for me : try right click on the current document and choose "format document with " , and choose your own format extension for the document. :)

  • This worked for me. I had correct prettier settings and the correct entries in .editorconfig but double quotes were not replaced with single ones on save. I fight clicked and chose Format Document With... and setup the default formatter from the two choices. Started working after that. – user882290 Jun 01 '23 at 16:01
1

I had a lot of issues controlling linting and prettier formating. I had rules for eslint on for prettier like

"prettier/prettier": [
      "error",
      { "singleQuote": true, "trailingComma": "none" }
    ],

and rules inside .prettierrc file

{
  "tabWidth": 2
}

But my .prettierrc file was not getting processed. My fix was installing prettier as a package on dev dependency. So the solution that worked for me was installing all these packages eslint-config-prettier eslint-plugin-prettier and prettier.

vishnu p s
  • 51
  • 6
1

If you're using a YAML plugin, it also has a single/double quote option that was tripping me up. Cheers. enter image description here

scott-joe
  • 169
  • 1
  • 5
0

"prettier": { "singleQuote": true },

This line of code save my hours.

israr
  • 332
  • 2
  • 4
0

The way I solve it is by disabling all formatting rules in eslint by installing npm i -D eslint-config-prettier then adding "prettier" at the end of extends array of eslint config file

module.exports = {
  env: {
    node: true,
  },
  extends: [
    'airbnb-base',
    'airbnb-typescript/base',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'prettier',

  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
  },
  plugins: ['@typescript-eslint'],
  root: true,
};

And then made prettier as my default formatter enter image description here

0

wow. none of the above worked. any text with apostrophe, when i type in 'bob\'s Burgers' , on save, turns into "bob's burgers", then eslint would complain. Prettier was the culprit. prettier would auto double quote, causing eslint to error.

{
  ...
  "singleQuote": true,
  "jsxSingleQuote": true,
}

What worked for me: disable prettier, and let eslint and VScode to all the work.

  1. go to ctrl +p.
  2. go to "> settings.json" here's my settings.json
{
  "editor.minimap.enabled": false,
  "redhat.telemetry.enabled": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": [
    "javascript"
  ],
  "[javascript]": {
    "editor.tabSize": 2,
    "editor.insertSpaces": true,
  },
  "editor.tabSize": 2,
  "workbench.tree.indent": 15,
  "editor.wordWrap": "on",
  "editor.foldingMaximumRegions": 50000,
  "search.exclude": {
    "**/*.bundle": true,
    "**/*.bundle.map": true,
    "**/dist": true,
    "**/mobile-gifts": true
  },
  "[python]": {
    "editor.formatOnType": true
  },
  "editor.inlineSuggest.enabled": true,
  "files.exclude": {
    "**/mobile-gifts": true
  },
  "editor.formatOnSave": true
}

and my eslintrc.js

// eslint-disable-next-line
module.exports = {
  env: {
    browser: true,
    es2021: true,
    jest: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react-hooks/recommended",
  ],
  overrides: [],
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module",
  },
  plugins: ["react"],
  rules: {
    "no-multiple-empty-lines": ["error", { max: 1, maxBOF: 0, maxEOF: 0 }],
    indent: ["error", 2],
    "react/react-in-jsx-scope": "off",
    camelcase: "off",
    "spaced-comment": "error",
    quotes: "off",
    "no-duplicate-imports": "error",
    semi: ["error", "never"],
    "react-hooks/exhaustive-deps": "off",
    "@typescript-eslint/no-explicit-any": "off",
  },
}

not_fubar_yet
  • 194
  • 15