EveryPage

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 .prettierignore file in the root of your project, which uses gitignore syntax. Prettier also follows rules in a .gitignore file 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-modules option is given.
    • Custom ignore file: The --ignore-path option points Prettier to a file of ignore patterns, and it replaces the default .gitignore and .prettierignore lookup 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 as prettier. "!**/*.{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​

  • 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 add before 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 write npx lint-staged into the pre-commit file. Then add a lint-staged entry to package.json that runs prettier --write --ignore-unknown on 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 --staged and run simple-git-hooks. This suits cases where you want entire files formatted on your changed or staged files.
    • Lefthook: Add a Prettier job under pre-commit in a lefthook.yml file at the repo root, running prettier --ignore-unknown --write on the staged files, with stage_fixed: true so the formatted versions are committed. Then install Lefthook and run lefthook install, and re-run it whenever you change lefthook.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​

  • How do I change the line width?

    You change the line width with the printWidth option. Its default is 80. On the command line, use --print-width <int>, and in the API, use printWidth: <int>. Setting max_line_length in an .editorconfig file also configures the print width, unless it is overridden.

    • --print-width <int>: the CLI override.
    • printWidth: <int>: the API override.
    • max_line_length: the .editorconfig setting.

    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 import statements and require calls on one line. It also keeps lengthy test descriptions in describe, it and test calls 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