prettier/docs/options.md

15 KiB
Raw Blame History

id title
options Options

Prettier ships with a handful of customizable format options, usable in both the CLI and API.

Print Width

Specify the line length that the printer will wrap on.

For readability we recommend against using more than 80 characters:

In code styleguides, maximum line length rules are often set to 100 or 120. However, when humans write code, they don't strive to reach the maximum number of columns on every line. Developers often use whitespace to break up long lines for readability. In practice, the average line length often ends up well below the maximum.

Prettier, on the other hand, strives to fit the most code into every line. With the print width set to 120, prettier may produce overly compact, or otherwise undesirable code.

See the print width rationale for more information.

Default CLI Override API Override
80 --print-width <int> printWidth: <int>

(If you don't want line wrapping when formatting Markdown, you can set the Prose Wrap option to disable it.)

Tab Width

Specify the number of spaces per indentation-level.

Default CLI Override API Override
2 --tab-width <int> tabWidth: <int>

Tabs

Indent lines with tabs instead of spaces.

Default CLI Override API Override
false --use-tabs useTabs: <bool>

(Tabs will be used for indentation but Prettier uses spaces to align things, such as in ternaries.)

Semicolons

Print semicolons at the ends of statements.

Valid options:

  • true - Add a semicolon at the end of every statement.
  • false - Only add semicolons at the beginning of lines that may introduce ASI failures.
Default CLI Override API Override
true --no-semi semi: <bool>

Quotes

Use single quotes instead of double quotes.

Notes:

  • JSX quotes ignore this option see jsx-single-quote.
  • If the number of quotes outweighs the other quote, the quote which is less used will be used to format the string - Example: "I'm double quoted" results in "I'm double quoted" and "This \"example\" is single quoted" results in 'This "example" is single quoted'.

See the strings rationale for more information.

Default CLI Override API Override
false --single-quote singleQuote: <bool>

JSX Quotes

Use single quotes instead of double quotes in JSX.

Default CLI Override API Override
false --jsx-single-quote jsxSingleQuote: <bool>

Trailing Commas

Print trailing commas wherever possible when multi-line. (A single-line array, for example, never gets trailing commas.)

Valid options:

  • "none" - No trailing commas.
  • "es5" - Trailing commas where valid in ES5 (objects, arrays, etc.)
  • "all" - Trailing commas wherever possible (including function arguments). This requires node 8 or a transform.
Default CLI Override API Override
"none" --trailing-comma <none|es5|all> trailingComma: "<none|es5|all>"

Bracket Spacing

Print spaces between brackets in object literals.

Valid options:

  • true - Example: { foo: bar }.
  • false - Example: {foo: bar}.
Default CLI Override API Override
true --no-bracket-spacing bracketSpacing: <bool>

JSX Brackets

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).

Valid options:

  • true - Example:
<button
  className="prettier-class"
  id="prettier-id"
  onClick={this.handleClick}>
  Click Here
</button>
  • false - Example:
<button
  className="prettier-class"
  id="prettier-id"
  onClick={this.handleClick}
>
  Click Here
</button>
Default CLI Override API Override
false --jsx-bracket-same-line jsxBracketSameLine: <bool>

Arrow Function Parentheses

available in v1.9.0+

Include parentheses around a sole arrow function parameter.

Valid options:

  • "avoid" - Omit parens when possible. Example: x => x
  • "always" - Always include parens. Example: (x) => x
Default CLI Override API Override
"avoid" --arrow-parens <avoid|always> arrowParens: "<avoid|always>"

Range

Format only a segment of a file.

These two options can be used to format code starting and ending at a given character offset (inclusive and exclusive, respectively). The range will extend:

  • Backwards to the start of the first line containing the selected statement.
  • Forwards to the end of the selected statement.

These options cannot be used with cursorOffset.

Default CLI Override API Override
0 --range-start <int> rangeStart: <int>
Infinity --range-end <int> rangeEnd: <int>

Parser

Specify which parser to use.

Prettier automatically infers the parser from the input file path, so you shouldn't have to change this setting.

Both the babylon 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 babylon.

Valid options:

Custom parsers are also supported. Since v1.5.0

Default CLI Override API Override
None --parser <string>
--parser ./my-parser
parser: "<string>"
parser: require("./my-parser")

Note: the default value was "babylon" until v1.13.0.

FilePath

Specify the input filepath. This will be used to do parser inference.

For example, the following will use the CSS parser:

cat foo | prettier --stdin-filepath foo.css
Default CLI Override API Override
None --stdin-filepath <string> filepath: "<string>"

Require pragma

available in v1.7.0+

Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file. This is very useful when gradually transitioning large, unformatted codebases to prettier.

For example, a file with the following as its first comment will be formatted when --require-pragma is supplied:

/**
 * @prettier
 */

or

/**
 * @format
 */
Default CLI Override API Override
false --require-pragma requirePragma: <bool>

Insert Pragma

available in v1.8.0+

Prettier can insert a special @format marker at the top of files specifying that the file has been formatted with prettier. This works well when used in tandem with the --require-pragma option. If there is already a docblock at the top of the file then this option will add a newline to it with the @format marker.

Default CLI Override API Override
false --insert-pragma insertPragma: <bool>

Prose Wrap

available in v1.8.2+

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".

Valid options:

  • "always" - Wrap prose if it exceeds the print width.
  • "never" - Do not wrap prose.
  • "preserve" - Wrap prose as-is. available in v1.9.0+
Default CLI Override API Override
"preserve" --prose-wrap <always|never|preserve> proseWrap: "<always|never|preserve>"

HTML Whitespace Sensitivity

available in v1.15.0+

Specify the global whitespace sensitivity for HTML files, see whitespace-sensitive formatting for more info.

Valid options:

  • "css" - Respect the default value of CSS display property.
  • "strict" - Whitespaces are considered sensitive.
  • "ignore" - Whitespaces are considered insensitive.
Default CLI Override API Override
"css" --html-whitespace-sensitivity <css|strict|ignore> htmlWhitespaceSensitivity: "<css|strict|ignore>"

End of Line

available in 1.15.0+

For historical reasons, there exist two commonly used flavors of line endings in text files. That is \n (or LF for Line Feed) and \r\n (or CRLF for Carriage Return + Line Feed). The former is common on Linux and macOS, while the latter is prevalent on Windows. Some details explaining why it is so can be found on Wikipedia.

By default, Prettier preserves a flavor of line endings a given file has already used. It also converts mixed line endings within one file to what it finds at the end of the first line.

When people collaborate on a project from different operating systems, it becomes easy to end up with mixed line endings in the central git repository. It is also possible for Windows users to accidentally change line endings in an already committed file from LF to CRLF. Doing so produces a large git diff, and if it get unnoticed during code review, all line-by-line history for the file (git blame) gets lost.

If you want to make sure that your git repository only contains Linux-style line endings in files covered by Prettier:

  1. Set endOfLine option to lf
  2. Configure a pre-commit hook that will run Prettier
  3. Configure Prettier to run in your CI pipeline (e.g. using prettier-check npm package)
  4. Ask Windows users to run git config core.autocrlf false before working on your repo so that git did not convert LF to CRLF on checkout. Alternatively, you can add * text=auto eol=lf to the repo's .gitattributes file to achieve this.

All modern text editors in all operating systems are able to correctly display line endings when \n (LF) is used. However, old versions of Notepad for Windows will visually squash such lines into one.

Valid options:

  • "auto" - Maintain existing line endings (mixed values within one file are normalised by looking at what's used after the first line)
  • "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
Default CLI Override API Override
"auto" --end-of-line <auto|lf|crlf|cr> endOfLine: "<auto|lf|crlf|cr>"