Disable prettier for a single file
Asked Answered
F

8

83

I need to disable prettier for a single file (API URLs file) in my project in Vs-code. actually, I need each API and its URL to be in one line, but prettier breaks them in two lines.

before

export const GET_SEARCH_TEACHERS = params => myexampleFunction_app_base(`teachers/search/${params.search}`);

after

export const GET_SEARCH_TEACHERS = params =>
myexampleFunction_app_base(`teachers/search/${params.search}`);
Felicidad answered 23/1, 2020 at 10:42 Comment(8)
May you post an example of what's happening?Bagasse
You might want to consult the documentation for questions like this. prettier.io/docs/en/ignore.html github.com/prettier/prettier/issues/3634Bagasse
based documentation you've mentioned there is a package 'Formatting Toggle' for vs code for do this , But I prefer not to install the new package.Felicidad
The github link mentions that ignoring a single file in VSCode is not trivial, so someone made an extension for it. If you don't want to install it, you'll likely need to implement your own.Bagasse
Or you could use a "prettier ignore" comment which will ignore those lines from being formattedBagasse
an example added.Felicidad
Can you give an example with prettier ignore or share a link that describe that with exampleFelicidad
I already have (please check the links in the comment above)Bagasse
F
95

If you want a certain file in a repo to never be formatted by prettier, you can add it to a .prettierignore file: Disable Prettier for one file

From the docs:

To exclude files from formatting, create a .prettierignore file in the root of your project. .prettierignore uses gitignore syntax.

Example:

# Ignore artifacts: 
build 
coverage

# Ignore all HTML files:
*.html 
Fetial answered 23/1, 2020 at 13:16 Comment(3)
and if you want to disable the whole thing just put ** in the prettierignore fileDestroyer
How can I disable single file without touching .prettierignore?Ligon
by adding this on top of your file - /* eslint-disable prettier/prettier */Fetial
F
58

Thanks to evolutionxbox, so far couple of solutions were found.

Ignoring Files or Folders

To exclude files from formatting, add entries to a .prettierignore file in the project root or set the --ignore-path CLI option. .prettierignore uses gitignore syntax.

/app/src/scripts/example.js
/app/src/folder/

Ignore based on extension

To exclude files based on extesntion you can add entries to a .prettierignore file as well

*.html.erb

Ignore lines

JavaScript

A JavaScript comment of // prettier-ignore will exclude the next node in the abstract syntax tree from formatting.

    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

    // prettier-ignore
    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

will be transformed to:

    matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

    // prettier-ignore
    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

JSX

    <div>
      {/* prettier-ignore */}
      <span     ugly  format=''   />
    </div>

more: https://prettier.io/docs/en/ignore.html

Using an extension

We can use an extension to toggle formatting like prettier on the specific page when you need it.

Formatting Toggle https://marketplace.visualstudio.com/items?itemName=tombonnike.vscode-status-bar-format-toggle

Felicidad answered 23/1, 2020 at 13:13 Comment(0)
A
11

Another option is to use the prettier block-like toggle, to disable formatting for a "block" within a file.
For example, adding // prettier-ignore before the start of a function definition, will disable prettier formatting for that function.
Similarly, if you put the line above an if statement, only the if block is ignored.

Basically a block is denoted by a pair of { } matching braces.

... (code up here is formatted by prettier)

// prettier-ignore
function noPrettierFormattingInHere(){
  ...
}

... (code down here is formatted by prettier)
Aggi answered 6/2, 2022 at 9:26 Comment(0)
S
5

create .prettierignore file in the root of your repo and add the name of the folders that you want to ignore and add full path of the file that you want to ignore and save it.

use the .gitignore format to update your file you can read about it in the prettier website too https://prettier.io/docs/en/ignore.html#ignoring-files

Soundproof answered 21/9, 2021 at 8:46 Comment(0)
D
1

. prettierignore for React project

build/
node_modules/
internals/generators/
internals/scripts/
package-lock.json
yarn.lock
package.json
coverage
Dancy answered 22/12, 2022 at 23:54 Comment(0)
R
1

You can just use this at the top of your file:

/* eslint-disable prettier/prettier */

Release answered 12/12, 2023 at 2:38 Comment(0)
G
1

If you're using Visual Studio Code and want to save a single file without adding anything to the .prettierignore or to the file itself, simply save the file using this shortcut:

CTRL + K CTRL + SHIFT + S

Glycogen answered 21/3 at 21:44 Comment(0)
P
-1

To disable Prettier for a specific file just add the following code

// prettier-ignore

Example:

const { gql } = require("@apollo/client");

// prettier-ignore

export const INSERT_RECHARGE_REGISTER = gql`

`
Prestige answered 30/6, 2023 at 7:16 Comment(1)
This only disables prettier for the next statement or block.Ligon

© 2022 - 2024 — McMap. All rights reserved.