benard-kong
benard-kong

Reputation: 2637

Disable ALL Auto Formatting on Save in VSCode

I'm editing someone else's code and I only want to change 1 line of a 9000 line file. But every time I save, VS Code formats the entire file and removes any trailing white space. This is a no-no because when I push this up, the reviewer will have no idea which line to look at.

I've tried disabling prettier, adding all files to .prettierignore, going into VS Code settings and disabling any suggestions of a formatter or white space trimming, turning off formatOnSave.

Here is my .vscode/settings.json

{
  "prettier.disableLanguages": [
    "js",
    "json",
    "javascript"
  ],
  "javascript.format.enable": false,
  "typescript.format.enable": false,
  "json.format.enable": false,
  "html.format.enable": false,
  "emmet.showAbbreviationSuggestions": false,
  "css.validate": false,
  "editor.defaultFormatter": null,
  "editor.formatOnSave": false,
  "[javascript]": {
      "editor.formatOnSave": false,
      "editor.defaultFormatter": null
  },
  "editor.trimAutoWhitespace": false,
  "diffEditor.ignoreTrimWhitespace": false,
  "files.trimTrailingWhitespace": false,
  "files.trimFinalNewlines": false,
  "eslint.format.enable": false,
  "files.autoSave": "off",
}

The only thing that seems to work is if I do CTRL + SHIFT + P, then SAVE WITHOUT FORMATTING. But what setting can I have so I can just do that with normal saving?

Upvotes: 163

Views: 204932

Answers (24)

sandeep talabathula
sandeep talabathula

Reputation: 3308

Search format and Un-Check Editor: Format On Save.

Editor: Format On Save

Upvotes: 1

sunice
sunice

Reputation: 59

Even if you have editor.formatOnSave disabled, the problem can persist if it was applied to the wrong settings file.

Use ctrl+shift+P and search for "Preferences: Open Settings (JSON)". In my case I had to apply the setting to "Open User Settings" instead of the Default/Workplace variants. This successfully stopped the auto-format/indent on saving.

settings screenshot

Upvotes: 6

Victor Timi
Victor Timi

Reputation: 433

If you're using prettier, on your VS Code type: CMD + SHIFT + P and search for: Preferences: Open User Settings (JSON) it's going to open a settings .json file, then set editor.formatOnSave to false:

"editor.formatOnSave": false,

editor.formatOnSave

Upvotes: 1

Arun Muthupalaniappan
Arun Muthupalaniappan

Reputation: 170

This is the only option that worked for me in VS Code 1.88.1 (Apr 2024). (deleting formatters and setting "editor.formatOnSave": false did not work, formatting toggle extension did not work either)

  1. Open Keyboard Shortcuts, (CMD+SHIFT+P for Macs, CTRL+SHIFT+P for Win -> Preferences: Open Keyboard Shortcuts)
  2. Change the shortcut for File: Save without Formatting to CMD+S or CTRL+S
  3. Change the shortcut for File: Save to some combination that is not usually used.

VS-Code keyboard shortcuts

Upvotes: 7

Sami Boudoukha
Sami Boudoukha

Reputation: 589

If you only want to disable formatting on save for the current file or periodicaly, you can hit on MacOS:

CMD + K, then S

Will save without formatting.

If you do not have this shortcut :

CMD + SHIFT + P , then search for Save , you'll have Save without Formatting option with the associated shortcut shown on the right.

Upvotes: 2

user23298959
user23298959

Reputation: 1

i found out that:

# fmt: off

Will not format this

# fmt: on

will start formatting again from here in vscode

Upvotes: -1

vladimir ulianitsky
vladimir ulianitsky

Reputation: 136

In my case it the reason was that the project I was editing has .vscode/settings.json with "editor.formatOnSave": true

Upvotes: 1

Seth Falco
Seth Falco

Reputation: 620

Save without Formatting

The ideal solution to this would be to use the "Save without Formatting" command.

This can be executed by either:

  • Selecting it in the command palette: CTRL + SHIFT + P
  • Doing the keyboard shortcut: CTRL + K, S

This lets you save files, bypassing any formatters that may run automatically.

Format on Save

You might want to disable auto-formatting entirely. You might just not like it, or because it can be annoying when paired with files.autoSave which saves automatically, and therefore formats automatically too.

First check your global settings and see if you have editor.formatOnSave enabled. If so, then disable it.

You can navigate to your settings by either:

  • Navigating the menu bar: FilePreferencesSettings
  • Doing the keyboard shortcut: CTRL + ,

Then search for editor.formatOnSave and disable it.

If it's still auto-formatting, then it might be enabled in workspace settings. Check if the root of the workspace has a file at .vscode/settings.json, and if so, if it has editor.formatOnSave. If it does, then either disable it there or delete the file to stop it from overriding your global settings.

Other Extensions

Check if any other extensions are performing code actions on save. Other answers here cover this topic a bit, but in general, you can navigate through your setting and search terms like "save" or "format".


All UI navigation and keyboard shortcuts are taken from the Linux installation, they may vary on other platforms.

Upvotes: 10

George Milroy
George Milroy

Reputation: 43

Switching off here solved my problem. enter image description here

Upvotes: 0

Soreg
Soreg

Reputation: 51

May not be for JS, but the EditorConfig for VS Code extension applies some formatting on save, including removing trailing whitespace (and it looks like you can't disable that, why?!)

Upvotes: 1

Shilei Mao
Shilei Mao

Reputation: 71

I had similar problem, tried disable all the installed extensions & confirmed format on save disabled, but still it's doing format every time when I hit control + s button. it's very annoying which breaks the source control a lot!

turns out I had a solution (not so pretty one I suppose ...), try bring up the command panel by clicking control + shift + p and search for save (or save without format) and click on the setting button enter image description here just swap the key binding for save with this command, that should do the trick! enter image description here

Upvotes: 3

DanL
DanL

Reputation: 101

I had this issue lurking for a long time. My solution was to initially enable "formatOnsave",

"editor.formatOnSave": true -> "editor.formatOnSave": false

Upvotes: 1

Jordy
Jordy

Reputation: 1960

For me, when I delete the JS-CSS-HTML Formatter VS Code Extension, this issue is gone. Steps:

  1. Open a file (in my case: HTML) and click HTML in the right bottom:

enter image description here

  1. Click Configure 'HTML' language based settings...
  2. Remove all object inside [html]:

enter image description here

Upvotes: 1

Hamed Moghadasi
Hamed Moghadasi

Reputation: 1673

You can do it by changing the user settings file:

  1. press ctrl + shift + p
  2. go to the preferences: Open User Settings(JSON)

you can disable the format code on save for all files by adding the below code:

{
    "editor.formatOnSave": false,
}

And if you wanna only disable formatting for specific files you can use the below code:

{
    "editor.formatOnSave": true,
    "[markdown]": 
    {
      "editor.formatOnSave": false
    },
    "[yaml]": {
       "editor.formatOnSave": false
    },
}

by using this code, formatting is disabled for yaml and markdown files but is enabled for the rest of the file formats.


hope this helps.

Upvotes: 13

Alexandra Chang
Alexandra Chang

Reputation: 11

I just got the same problem and while trying these solutions, I realized that when you click ctrl + shift + P, and type "Save without formatting", the option will have a settings icon on the right corner, click there to configure keybinding, you can put any keybinding you want there, for example ctrl + k, which I put. It worked instantly and it was very easy, so you can have the best of both worlds.

Upvotes: 1

Aryav Bhola
Aryav Bhola

Reputation: 349

In my case, I uninstalled prettier and was using html-css-js code formatter. I had already unchecked format on save, and had tried many other methods. I finally changed a setting in formatter.json (settings of html-css-js formatter) and then it worked.

Steps:

  1. ctrl + shift + P
  2. Formatter Config
  3. If "onSave": true, change true to false
  4. Save the formatter.json and reload window.

My formatter.json settings

This worked for me. I can use auto-save after this as well.

Upvotes: 17

David Carter
David Carter

Reputation: 69

Go to C:\Users[user]\AppData\Roaming\Code\User

and change the editor.formatOnSave option to false.

editing settings.json

Upvotes: 0

Ares
Ares

Reputation: 431

In my case I had two default code formatters that had conflicting settings, "prettier" was set not to format on save but JS-CSS-HTML Formatter was still set as my default formatter for some file types. I suggest using only one formatter and removing any unused / conflicting ones from your extensions list.

The way I used was to:

  1. Open a file type ( one that's giving you formatting on save issues. )

  2. Click on the file type and select "Configure 'yourFiletype' language based settings.. from the dropdown. enter image description here

  3. Delete the line that sets your default formatter: ex) "editor.defaultFormatter": "lonefy.vscode-JS-CSS-HTML-formatter" enter image description here

  4. Then go back to your file and try formatting normally, ( my hotkey is: alt + shift+ f ), it should prompt you to select your default formatter.

This should help minimize conflicting formatters issues.

Upvotes: 8

Timo
Timo

Reputation: 196

I've just stumbled across this question while experiencing the exact same problem. The way I solved it, was just to switch the language of the document to plain text (you can do so by clicking on the language in the bottom navigation bar). If you then hit "save", no reformatting happens.

Disadvantage: You get no syntax highlighting. But if you just want to fix a typo or something and don't want to mess with the settings or install an extension, it's probably an ok workaround.

Upvotes: 5

checkit
checkit

Reputation: 1576

Had the same problem, just bind 'cmd + s' to saving without formatting. press cmd+shift+p then search for save without formatting and click on the configure icon, then bind it with 'cmd + s', problem gone :)

Upvotes: 156

csgeek
csgeek

Reputation: 801

I came across this problem a while back. I disabled the formatOnSave option in settings.

Goto File > Preferences > Settings or Ctrl + ,. In User tab choose Text editor and navigate

to Formatting, disable the formatOnSave option.

YW!

Upvotes: 13

Moriya
Moriya

Reputation: 7906

Perhaps this plugin called Formatting Toggle by tombonnike can help you.

It disables the autosave with a toggle.

From its description:

A VS Code extension that allows you to toggle the formatter (Prettier, Beautify, …) ON and OFF with a simple click.

Upvotes: 6

Romel Pérez
Romel Pérez

Reputation: 917

In the case it is ESLint, and not Prettier, the solution is:

Open Preferences: Open Settings (JSON) and configure:

"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
    "source.fixAll": false,
    "source.fixAll.eslint": false
}

I don't have Prettier but the solution may be similar.

Upvotes: 31

Guillermo Brachetta
Guillermo Brachetta

Reputation: 4745

Did you try adding

"editor.formatOnSave": false

in your user settings rather than in your project settings?

Upvotes: 90

Related Questions