Reputation: 2637
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
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.
Upvotes: 6
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,
Upvotes: 1
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)
CMD+SHIFT+P
for Macs, CTRL+SHIFT+P
for Win
-> Preferences: Open Keyboard Shortcuts
)File: Save without Formatting
to CMD+S
or CTRL+S
File: Save
to some combination that is not usually used.Upvotes: 7
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
Reputation: 1
i found out that:
Will not format this
will start formatting again from here in vscode
Upvotes: -1
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
Reputation: 620
The ideal solution to this would be to use the "Save without Formatting" command.
This can be executed by either:
CTRL
+ SHIFT
+ P
CTRL
+ K
, S
This lets you save files, bypassing any formatters that may run automatically.
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:
File
→ Preferences
→ Settings
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.
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
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
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 just swap the key binding for save with this command, that should do the trick!
Upvotes: 3
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
Reputation: 1960
For me, when I delete the JS-CSS-HTML Formatter VS Code Extension, this issue is gone. Steps:
HTML
) and click HTML
in the right bottom:Configure 'HTML' language based settings...
[html]
:Upvotes: 1
Reputation: 1673
You can do it by changing the user settings file:
ctrl + shift + p
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
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
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:
This worked for me. I can use auto-save after this as well.
Upvotes: 17
Reputation: 69
Go to C:\Users[user]\AppData\Roaming\Code\User
and change the editor.formatOnSave option to false.
Upvotes: 0
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:
Open a file type ( one that's giving you formatting on save issues. )
Click on the file type and select "Configure 'yourFiletype' language based settings.. from the dropdown.
Delete the line that sets your default formatter: ex) "editor.defaultFormatter": "lonefy.vscode-JS-CSS-HTML-formatter"
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
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
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
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
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
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
Reputation: 4745
Did you try adding
"editor.formatOnSave": false
in your user settings rather than in your project settings?
Upvotes: 90