How to prevent auto save when executing code in Visual Studio - visual-studio-code

When I run my code in Visual Studio, it auto saves my code -without prompting (!)- even though I have auto-save set to off. Is there a way to prevent this?
I've made sure auto-save is off in preferences. I haven't seen any setting that applies specifically to "on execute"
As an example of how this made me lose some hair, I opened a file that had some unknown characters (which opened just fine in Powershell ISE) and I didn't know until the code failed when I executed it. And since it auto-saved on execute, it wrote the bad characters to the file, corrupting it for all eternity. Not to mention, if I modify a few characters to test something, I don't want it to auto-save for me.

Search for autosave under File-> Preferences - Settings and turn it off

Related

Configure VSCode to ask to save modified files on exit

I'd like to configure VS code to work the same as pretty much every editor I've ever used which is to save only when I tell it to and to ask to save modified files if I try to quit.
I've set Auto Save to "off"
I've set Hot Exit to "off"
As it is if I have modified files and I choose quit it just exits immediately, losing all my changes. I could have sworn this used to work.
version 1.41.1
At least, VSCode 1.51 (Oct. 2020) mitigates that situation with:
Prevent accidental close
A new setting window.confirmBeforeClose was added to show a confirmation dialog before closing or leaving the workbench.
Possible values are:
keyboardOnly: The confirmation will only be shown when you use a keybinding to close (for example Ctrl+W or Cmd+W). (default)
always: The confirmation dialog will always be shown, even if you close from a mouse gesture.
never: The confirmation will never be shown.
But, that was only for Web.
VSCode 1.66 (Apr. 2022) will propose the same for the desktop app.
See issue 14710 and PR 147242
The issue was if you're in extension develop mode it just exits. If I pass in --extensionDevelopmentPath some/path then the save options are ignored. Without that it works as it's supposed to.
Apparently that's by design
https://github.com/microsoft/vscode/issues/71405

How to temporarily disable "preventing dirty writes" feature in Visual Studio Code

In my project, I've used some build/linting tools so they can automate force change my code based on some convention code styles but when I was running those scripts, vs code showed an error message:
Failed to save "<file-name>": The content on disk is newer. Please compare your version with the one on disk.and continue to open the "Resolve to save conflict" tab.
It seems to be annoying and not convenient for me.
So can you tell me how to disable this feature of VScode for a temporarily time?
v1.42 is modifying this functionality including adding a setting so that the dirty file/save conflict notification can be disabled. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#ignore-save-conflicts
Ignore save conflicts
VS Code has a built-in mechanism to show an error when you try to save
a dirty file that has been changed outside of VS Code or by another
program (for example Git):
The motivation is to inform you that saving will overwrite the
contents on disk even though the file was changed after the file
became dirty. Clicking the Compare button lets you review your change
along with the version on disk and either overwrite or revert the
file.
In cases where you know that the changes on disk can be overwritten,
there is now an Overwrite button directly on the notification. You can
also configure files.saveConflictResolution globally or per workspace or
file type to disable the notification altogether. The options are askUser (default) or overwriteFileOnDisk.

How to refresh content of opened files in VS Code?

I'm looking for a way to refresh the content of a file opened in editor after it was changed by other tool.
For instance I'm using angular command line which makes changes to files but VS Code doesn't refresh the content after that and I can't see any of that. What's worse when I try to save the file, VS Code by default tries to save the old content upon the newer (asks before of course, but still its not what I'm looking for).
I didn't find any keybindings for refresh the editor's content. Maybe there are any extensions?
Thank you.
It should reload itself automatically, but I had a machine that stopped auto-refreshing. I'd change the file externally (with vim or whatever), and the file I had in the editor wouldn't pop and refresh itself, which it used to do. It turned out to be a git-related extension. I disabled it, and autorefresh returned. If you haven't already, try it with extensions off.

vscode: Automatically save all on build

In Visual Studio when I trigger a build it automatically saves all files first. I want the same behavior in vscode, but couldn't find a way to do it short of writing my own extension. Is there any simple method I'm missing?
It's not exactly what you're asking, but will also accomplish what you want: you could enable the auto-save feature:
"files.autoSave": "afterDelay"
After using IntelliJ for a while, I managed to get rid of my habit of pressing Ctrl+S after every second typed-out word, for the most part (since it has auto-save by default). I was happy to find out that VSCode support this somewhat less stressful workflow too.
Other than that, there's also a Files: Save All command you've probably already found.
I'm actually not sure you can do this with the extension API right now - you can definitely trigger the "save all" command easily, but you'd need to be notified of when a build occurs, and I couldn't find any events for this. I think it will definitely be possible after #15179 is resolved though, which should allow extensions to participate in builds.

How to setup Visual Studio Code to detect and set the correct encoding on file open

I recently started to use Visual Studio Code on Server Systems where I did not have Studio IDE installed. I like it very much but I'm running into a problem.
When I open a file (used Notepad++ before) the editor detects the encoding and sets it for me. I have many files on windows servers that are still with windows-1252 but vscode just uses UTF-8 by default.
I know I can reopen with encoding Western (Windows 1252) but I often forget it and I have sometimes destroyed some content while saving it.
So I did not find any parameter yet, is there a way to make vscode detect the encoding and set it automatically when I open a file?
To allow Visual Studio Code to automatically detect the encoding of a file, you can set "files.autoGuessEncoding":true (in the settings.json configuration file).
https://github.com/Microsoft/vscode/pull/21416
This obviously requires an updated verison of the application compared to when the question was originally asked.
Go to File-> Preferences -> User Settings
Add (or update) the entry "files.encoding": "windows1252" to the right editor window and save
Now VSCode opens all text files using windows-1252 when there is no proper encoding information set.
EDIT:
In 2017's June release the files.autoGuessEncoding setting was introduced. When enabled it will guess the file's encoding as good as possible. Its default value is false .
Add guide by image :
File >> Preferences >> Settings
Enter autoGuessEncoding and make sure checkbox is checked
beware, auto guessing in vscode still does not work as expected, the guessing, is VERY inaccurate, and does still open as guessed encoding, even when the guess library returns also the confidence score being low - they use jschardet (https://www.npmjs.com/package/jschardet)
if the score of guess is not close to 100%, it simply should rather open in "files.encoding" instead of guessed encoding, but that does not happen, vscode authors should make better use of the confidence score jschardet returns
i open either utf-8 which guesses ok, and second encoding i use is windows-1250, which in 99% cases detects wrong as some other windows-... encoding or even iso-8859-... and such... cry daily at the computer having to experience this
tuning up the confidence checking and fallback to default encoding would do, it needs someone skilled to check their source and offer them a fix
From Peminator's answer:
Beware: auto guessing in VSCode still does not work as expected, the guessing, is VERY inaccurate,
This should slightly improve with VSCode 1.67 (Apr. 2022) with (insider released):
Allow to set files.encoding as language specific setting for files on startup
we now detect changes to the editor language and there is almost always a transition from plaintext in the beginning until languages are resolved to a target language
if we detect that the new language has a configured files.encoding override
and that encoding is different from the current encoding
and the editor is not dirty or in save conflict resolution
:
we reopen the file with the configured encoding
Unfortunately I cannot tell apart this from happening vs. the user changing the language from the editor status bar.
So if the user changes language mode to bat from to ps1 and ps1 has a configured encoding, the encoding will change.