ChatGPT解决这个技术问题 Extra ChatGPT

How to prevent Visual Studio Code from always reopening the previous files or folders?

Visual Studio Code always seems to remember my session and reopen the files and/or projects that were open the last time I used it. It obviously behaves correctly when running it from the command line with a file or folder supplied, but when opening from a taskbar shortcut, I'd like it to default to an empty environment.

Is there any way to change this behavior?

How?? My VSCode never remembers previous sessions. It always opens with a brand new Untitled-1 tab. It drives me nuts
@Green Currently it only remembers previous files if you have a folder/workspace opened. So just open any random folder (i chose my desktop folder) and now it remembers any file between sessions, even those not from that folder.
A big fat +1 from me because this question actually revealed to be that VS code CAN RESTORE previous session. XD Also in terms of what @Cardin states things have changed. I have the latest VS code running on Win10 and setting "window.reopenFolders" : "all" opens every single file no matter if it is in the currently active directory or not. Sadly it seems that VS code still doesn't support having multiple active directories because of all that "active directory = workspace".
@rbaleksandar That's still a negative. VS Code does not reopen existing non-dirty files as of ver 1.11.2, even with "window.reopenFolders" : "all". To test, 1) go to File>Close Folder, 2) drag and drop any random file into VS Code, 3) go to File>Exit, 4) Relaunch VS Code, 5) Notice you get a blank VS Code editor again. In order to restore sessions, you need to have an active directory open. Any session, regardless of file location, will be remembered as long as you have an active directory. Btw, you can install Project Manager extension to jump between multiple active directories.
Be aware, you can lose work with this feature, at least in v1.19.3.

g
ghlecl

You can also go into your settings and use the following:

"window.reopenFolders": "none"

which will not reopen the folders you were working on when you closed the editor. The other options are one (the default) and all.

Edit 2017-11-09:

The option is now changed in latest versions.

"window.restoreWindows": "none"

See Mathieu DOMER's answer.

Edit 2018-09-12:

Another setting related to this is the hotExit setting. This has been discussed in this answer to a related question. To prevent reopening and remembering unsaved files, you can set this to:

"files.hotExit": "off"

But from the test I've made, when the window.restoreWindows setting is set to none, this is not needed. I haven't tested every possible combination, so YMMV.

And to answer a question in the comments, to edit the settings, you have to open the settings file. Some documentation can be found here (at least on the date I am writing this).

Edit 2022-03-16:

If you prefer using a GUI to change the settings, see D'Arcy Rittich's answer.


I'm changing the accepted answer to this one, as I feel like the settings approach is better than a command line switch, which only affects the one shortcut.
Folders? I don't use folders. How about just files?
I don't have window.reopenFolders as a setting option. Instead I have window.restoreWindows
A thing that might annoy some: VS Code will still restore the workspace if it contained a tab with an unsaved file.
This isn't working for me when I open a folder with code . command. Always restores open editors and I can't stand it
D
D'Arcy Rittich

In VS Code:

for Windows/Linux Ctrl+, (or choose File/Preferences/Settings) to open the settings page.

for Mac ⌘+, (or choose Code -> Preferences -> Settings) to open the settings page.

then type restoreWindows in the Search settings input to filter for this setting. Set it to none and restart VS Code.

https://i.stack.imgur.com/TccTQ.png


Worked Perfectly
This should be marked as answer, after failed with JSON setting I try this and it works! Thanks sir!
K
Kirk Woll

With latest update, it seems that the parameter has changed, now use:

"window.restoreWindows": "none"

B
Benjamin Pasero

You can add the -n option to the startup of VS Code and it will always start with an empty window, not restoring your previous session.


The option is definitively there (code --help displays -n, --new-window Force to open a new window.) but it still reopens all previous windows. I think it's only meant to do something meaningful when the program is already running.
where is the startup? I cannot find where to add the -n. Is it a file? Can I access it via Settings?
Only this worked for me on MacOs: "code -n ~/myFolder/myCodeFolder"
S
Sampath

01 December 2018

This works for me. i.e. "C:\Users\Sampath\AppData\Local\Programs\Microsoft VS Code\Code.exe" -n

https://i.stack.imgur.com/tcUve.png


V
Vatsal Shah

Below worked for me

Right-click on Shortcut and add --disable-gpu to Target as per screen shot.

https://i.stack.imgur.com/SYKnG.png


This works for me in both regular vs code and vs code insiders
W
Wak Joko

For me, none of above is working while I'm trying to close "dirty" unsaved files which I accidentally edited 1000 files and wanted to ignore saving all of them.

My fix was adding this line into settings.json:

    "files.hotExit": "off"

Open up vscode, close vscode and just click the confirmation button to close all of the files without saving.

Then open back vscode and boom.. no more unsaved files being shown.


Where did you find settings.json?
@CB_Ron you can find your settings.json file here: code.visualstudio.com/docs/getstarted/…
K
Khaled Alam

If "window.restoreWindows": "none" not solve the problem,
then try to run code as root -> sudo code --user-data-dir code files and restart code normally without root.


u
user14756878

For me the only solution that worked was to go to the solution root and delete the .vs folder.


By "solution", I assume you mean Visual Studio? Because the question was for Visual Studio Code.
M
Murali kondapi

I reinstalled Visual Studio Code by downloading the latest update. I did not have to uninstall the previously installed Code. It work ok for me now.