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:
"[javascript]": { "editor.formatOnSave": false }.vscode/settings.json folder in subfolders, but that's not supported/* eslint-disable prettier/prettier */ in my file (AFAIK prettier is my formatter for TypeScript files), and read through this open prettier issue on GitHub.prettierignore file and add my fileNone of these worked.
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.
Just save your file with:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With