Unofficial example
Ask the Prettier docs
The Ask this site box, reading prettier.io/docs. Type a question and it answers from those pages, with links.
Loading the box…
An unofficial example. Ask this site is not affiliated with or endorsed by the Prettier project. The Prettier docs belong to their authors and are read here under their MIT licence.
Questions it answered
Real answers from this box.
How do I ignore a file?
To exclude a file from formatting, add it to a
.prettierignorefile in the root of your project, which uses gitignore syntax. Prettier also follows rules in a.gitignorefile if one exists in the directory it is run from. Use "prettier-ignore" comments if you only want to ignore parts of a file rather than the whole file.Some related conditions apply:
- Default ignores: Prettier already ignores version control directories (".git", ".jj", ".sl", ".svn", ".hg") and
node_modules, the latter unless the--with-node-modulesoption is given. - Custom ignore file: The
--ignore-pathoption points Prettier to a file of ignore patterns, and it replaces the default.gitignoreand.prettierignorelookup rather than adding to it, so you must list those files again to keep them. - One-off commands: To skip files without editing
.prettierignore, you can use negative patterns on the command line, such asprettier. "!**/*.{js,jsx,vue}" --write.
Answered from Ignoring Code · Prettier — Ignoring Files: .prettierignore, Ignoring Code · Prettier, CLI · Prettier — --ignore-path, Ignoring Code · Prettier — Command Line File Patterns
- Default ignores: Prettier already ignores version control directories (".git", ".jj", ".sl", ".svn", ".hg") and
How do I run Prettier before each commit?
You can run Prettier as a pre-commit hook, which re-formats files marked as staged with
git addbefore you commit. This makes sure all commits are formatted without waiting for your CI build. Several setups are documented:- husky and lint-staged: Install both packages, run
npx husky init, and writenpx lint-stagedinto the pre-commit file. Then add alint-stagedentry topackage.jsonthat runsprettier --write --ignore-unknownon all files. If you use ESLint, make sure lint-staged runs it before Prettier, not after. - pretty-quick: Install simple-git-hooks and pretty-quick, then set the pre-commit hook to
pretty-quick --stagedand runsimple-git-hooks. This suits cases where you want entire files formatted on your changed or staged files. - Lefthook: Add a Prettier job under
pre-commitin alefthook.ymlfile at the repo root, runningprettier --ignore-unknown --writeon the staged files, withstage_fixed: trueso the formatted versions are committed. Then install Lefthook and runlefthook install, and re-run it whenever you changelefthook.yml. This option is useful if you also want hooks for other languages in the same repo.
Answered from Pre-commit Hook · Prettier, Install · Prettier — Git hooks, Pre-commit Hook · Prettier — Option 2. pretty-quick, Pre-commit Hook · Prettier — Option 5. Lefthook
- husky and lint-staged: Install both packages, run
How do I change the line width?
You change the line width with the
printWidthoption. Its default is80. On the command line, use--print-width <int>, and in the API, useprintWidth: <int>. Settingmax_line_lengthin an.editorconfigfile also configures the print width, unless it is overridden.--print-width <int>: the CLI override.printWidth: <int>: the API override.max_line_length: the.editorconfigsetting.
The width is a target, not a hard limit. Prettier will make both shorter and longer lines, but it generally tries to meet the specified width. Some content cannot be broken across lines, such as very long string literals, regexps, comments and variable names. Prettier also intentionally exceeds the width in some cases. It keeps single-element
importstatements andrequirecalls on one line. It also keeps lengthy test descriptions indescribe,itandtestcalls on one line.Markdown prose is a separate case. By default, Prettier does not change prose wrapping in Markdown. To wrap prose to the print width, set the Prose Wrap option to
"always".Answered from Options · Prettier — Print Width, Rationale · Prettier — Print width, Options · Prettier — Prose Wrap
Add this to your own docs
Paste your docs address to try it, free and without signing up. Then it is one line of code.
DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow