Prettier (code formatter) for the VS Code. To see the difference between official Prettier extension you can take a look at the CHANGELOG.
JavaScript
JSX Flow TypeScript JSON |
CSS
Less SCSS styled-components styled-jsx |
Markdown
CommonMark GitHub-Flavored Markdown MDX |
Plugins
PHP Pug Ruby Swift XML |
HTML
Vue Angular |
GraphQL
GraphQL Schemas |
YAML |
Install through VS Code extensions, search for Prettier+
by Benas Svipas
. If you can't find extension by name try to search by publisher name.
⚠ If you have any other code formatting extensions installed they might take precedence and format your code instead of Prettier+ leading to unexpected results.
1. Format Document With... -> Prettier+
2. Format Selection With... -> Prettier+
Mac: Shift + Option + F
Windows: Shift + Alt + F
Linux: Ctrl + Shift + I
// Format all files on save.
"editor.formatOnSave": true,
// Format per-language file on save.
"[javascript]": {
"editor.formatOnSave": false
}
These dependencies are bundled with the extension:
After installing this extension you can immediately start to format your code, you don't need to do anything additionally. But if you want to include some integrations or plugins, continue to read below.
- Install
eslint
locally with npm or Yarn. - Setup your ESLint configuration.
- Add
"prettier.eslintIntegration": true
in VS Code settings.
- Install
tslint
locally with npm or Yarn. - Setup your TSLint configuration.
- Add
"prettier.tslintIntegration": true
in VS Code settings.
- Install
stylelint
locally with npm or Yarn. - Setup your stylelint configuration.
- Add
"prettier.stylelintIntegration": true
in VS Code settings.
If you have both "prettier.eslintIntegration"
and "prettier.tslintIntegration"
enabled in your VS Code settings, then TSLint will be used to lint your TypeScript code. If you would rather use ESLint, disable the TSLint integration by setting "prettier.tslintIntegration"
to false
.
⚠ The plugin API is in a beta state. This extension supports only official plugins.
- PHP: install
prettier
and@prettier/plugin-php
locally or globally with npm or Yarn. - Pug: install
@prettier/plugin-pug
locally or globally with npm or Yarn. - Ruby: install
@prettier/plugin-ruby
locally or globally with npm or Yarn. - Swift: install
prettier/plugin-swift
locally or globally with npm or Yarn. - XML: install
@prettier/plugin-xml
locally or globally with npm or Yarn.
Prettier settings
Settings will be read from (listed by priority):
- Prettier config file
.editorconfig
Or if there's no Prettier config file:
.editorconfig
- VS Code settings (described below with their defaults)
Specify the line length that the printer will wrap on. Learn more here.
Specify the number of spaces per indentation-level. Learn more here.
Use single quotes instead of double quotes. Learn more here.
Print trailing commas wherever possible when multi-line. (A single-line array, for example, never gets trailing commas.) Learn more here.
Valid options:
"es5"
- Trailing commas where valid in ES5 (objects, arrays, etc.)"none"
- No trailing commas."all"
- Trailing commas wherever possible (including function arguments). This requires node 8 or a transform.
Print spaces between brackets in object literals. Learn more here.
Put the >
of a multi-line JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements). Learn more here.
Specify which parser to use. Learn more here.
⚠ Prettier automatically infers the parser from the input file path, so you shouldn't have to change this setting.
Both the babel
and flow
parsers support the same set of JavaScript features (including Flow type annotations). They might differ in some edge cases, so if you run into one of those you can try flow
instead of babel
.
Valid options:
""
- Automatically infers the parser from the input file path."babel"
- Via@babel/parser
namedbabylon
until v1.16.0"babel-flow"
- Same asbabel
but enables Flow parsing explicitly to avoid ambiguity. First available in v1.16.0"babel-ts"
- Similar totypescript
but uses Babel and its TypeScript plugin. First available in v2.0.0"flow"
- Viaflow-parser
"typescript"
- Via@typescript-eslint/typescript-estree
. First available in v1.4.0"css"
- Viapostcss-scss
andpostcss-less
, autodetects which to use. First available in v1.7.1"scss"
- Same parsers ascss
, preferspostcss-scss
. First available in v1.7.1"less"
- Same parsers ascss
, preferspostcss-less
. First available in v1.7.1"json"
- Via@babel/parser parseExpression
. First available in v1.5.0"json5"
- Same parser asjson
, but outputs asjson5
. First available in v1.13.0"json-stringify"
- Same parser asjson
, but outputs likeJSON.stringify
. First available in v1.13.0"graphql"
- Viagraphql/language
. First available in v1.5.0"markdown"
- Viaremark-parse
. First available in v1.8.0"mdx"
- Viaremark-parse
and@mdx-js/mdx
. First available in v1.15.0"html"
- Viaangular-html-parser
. First available in 1.15.0"vue"
- Same parser ashtml
, but also formats vue-specific syntax. First available in 1.10.0"angular"
- Same parser ashtml
, but also formats angular-specific syntax viaangular-estree-parser
. First available in 1.15.0"lwc"
- Same parser ashtml
, but also formats LWC-specific syntax for unquoted template attributes. First available in 1.17.0"yaml
- Viayaml
andyaml-unist-parser
. First available in 1.14.0
Print semicolons at the ends of statements. Learn more here.
Indent lines with tabs instead of spaces. Learn more here.
By default, Prettier will wrap markdown text as-is since some services use a linebreak-sensitive renderer, e.g. GitHub comment and BitBucket. In some cases you may want to rely on editor/viewer soft wrapping instead, so this option allows you to opt out with "never"
. Learn more here.
Valid options:
"preserve"
- Wrap prose as-is. First available in v1.9.0"always"
- Wrap prose if it exceeds the print width."never"
- Do not wrap prose.
Include parentheses around a sole arrow function parameter. Learn more here.
Valid options:
"always"
- Always include parens. Example:(x) => x
"avoid"
- Omit parens when possible. Example:x => x
Use single quotes instead of double quotes in JSX. Learn more here.
Specify the global whitespace sensitivity for HTML files. Learn more here.
Valid options:
"css"
- Respect the default value of CSSdisplay
property."strict"
- Whitespaces are considered sensitive."ignore"
- Whitespaces are considered insensitive.
Whether or not to indent the code inside <script>
and <style>
tags in Vue files. Some people (like the creator of Vue) don’t indent to save an indentation level, but this might break code folding in your editor. Learn more here.
Valid options:
"false"
- Do not indent script and style tags in Vue files."true"
- Indent script and style tags in Vue files.
Specify the end of line used by Prettier. Learn more here.
Valid options:
"lf"
- Line Feed only (\n
), common on Linux and macOS as well as inside git repos"crlf"
- Carriage Return + Line Feed characters (\r\n
), common on Windows"cr"
- Carriage Return character only (\r
), used very rarely"auto"
- Maintain existing line endings (mixed values within one file are normalised by looking at what's used after the first line)
Change when properties in objects are quoted. Learn more here.
Valid options:
"as-needed"
- Only add quotes around object properties where required."consistent"
- If at least one property in an object requires quotes, quote all properties."preserve"
- Respect the input use of quotes in object properties.
VS Code specific settings
These settings are specific to VS Code and need to be set in the VS Code settings file. See the documentation for how to do that.
Use prettier-eslint to format JavaScript, TypeScript and Vue.
Use prettier-tslint to format TypeScript.
Use prettier-stylelint to format CSS, SCSS and Less.
Require a config file to format code.
Path to a .prettierignore
or similar file such as .gitignore
. Files which match will not be formatted. Set to null
to not read ignore files. Restart required.
List of languages IDs to ignore. Restart required. Disabling a language enabled in a parent folder will prevent formatting instead of letting any other formatter to run.
Feel free to open issues or PRs!