Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude one specific file from "format on save" in VSCode?

There is a question about suppressing editor.formatOnSave for specific file extensions (languages) but I have a different question instead: how can I suppress this feature for one specific file in my workspace?

I want to suppress it in the repository somehow, with a config or hint in a comment, or some such, so I can prevent anyone from accidentally doing it wrong. I cannot tell my 20ish teammates to always "save without formatting", nor could I myself remember that at all times.

I have a ./vscode/settings.json file that has:

"editor.formatOnSave": true,

and it works wonderfully for all files in my workspace. However, I want to suppress it for my app-routing.module.ts file (it's an Angular project) because the auto-formatting there hurts readability more than it helps.

How to disable editor.formatOnSave for one specific file?

When I or a colleague use "Save" (CTRL + S on Windows) or "Save All" (CTRL + K, S on Windows) the auto formatting should not be triggered for that file.


What I've tried:

  • Check all answers in the related question (so e.g. "[javascript]": { "editor.formatOnSave": false }
  • Have a .vscode/settings.json folder in subfolders, but that's not supported
  • Include /* eslint-disable prettier/prettier */ in my file (AFAIK prettier is my formatter for TypeScript files), and read through this open prettier issue on GitHub
  • Create a .prettierignore file and add my file

None of these worked.


Workaround

Almost two years later, and I noticed that if you place // prettier-ignore inside a file it will recursively ignore the next line and all statements that are 'children' of that line. So if you place it just above -say- your @NgModule({ opening line, the entire class will be ignored.

For example:

// prettier-ignore
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [
    RouterModule
  ],
})
export class AppRoutingModule {
  nonprettymethod1() { console.log('Test'); }
  nonprettymethod2() {
    console.log('A very long line that would cause prettier to wrap it along mulitple lines as best it can.');
  }
}

Combined with these .vscode/settings.json settings:

{
  "[html]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
    "editor.formatOnSave": false
  },
  "[typescript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint",
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
    "editor.formatOnSave": false
  },
}

should do the trick.

like image 990
Jeroen Avatar asked Dec 18 '25 23:12

Jeroen


1 Answers

Just save your file with:

  • ControlK then S on Linux and Windows
  • ⌘ CommandK then S on Mac
like image 65
DevonDahon Avatar answered Dec 22 '25 00:12

DevonDahon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!