Visual Studio Code never reopens previous file or folder

Visual Studio-Code

Visual Studio-Code Problem Overview


I have the exact opposite Problem of this question: https://stackoverflow.com/questions/31270774/visual-studio-code-always-reopens-previous-file-or-folder

VSCode never seems to remembery my previously opened files and does always start with an empty window. I checked, if my Shortcut has the Parameter "-n" by accident (which would force this behaviour), but it's not there.

Is there a way to force VSCode to remember opened files and folders?

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

It seems, VsCode only remembers opened files, if you also have a folder opened. It doesn't matter, if this is just a Dummy Folder somewhere on your drive - if you open files outside of this folder, they will be remembered aswell.

I'm not sure if this is as designed, or a bug.

Edit: This is a bug and tracked here

Edit 2: To sum up the answers from below there are now two settings concerning this issue:

Hot exit has been implemented, which restores files which weren't saved before VS Code is closed. To change this, use this setting:

files.hotExit: "onExit"

To reopen all files, use this setting:

window.restoreWindows: "all"

But there is still on open issue with this found here.

...

Solution 2 - Visual Studio-Code

Go to: Open File --> Preferences --> Settings and add "window.restoreWindows": "all" to your settings.json file. Remember to add a comma to the preceding statement.

This will reopen all the windows you had open on the close of your last session.

Solution 3 - Visual Studio-Code

You could try the settings option:

"window.reopenFolders": "all"

This should reopen everything that was opened when you closed the editor, but I haven't tested it. I usually prefer that nothing gets reopened, so I use none. The default is one.

Solution 4 - Visual Studio-Code

They added this functionality in November 2016.

Here is the setting for changing hot exit behavior:

"files.hotExit": "onExit"

Solution 5 - Visual Studio-Code

So I needed that functionally too, currently it's just

"window.restoreWindows": "none",

in the settings.

Controls how windows are being reopened after a restart. Select 'none' to always start with an empty workspace, 'one' to reopen the last window you worked on, 'folders' to reopen all windows that had folders opened or 'all' to reopen all windows of your last session.

Solution 6 - Visual Studio-Code

I have tried the window.reopenFolders options, and found that the folder(s) and file(s) will re-open if starting VSCode from the desktop icon, but if I click on a file in explorer and it uses VSCode to open (and VSCode is not currently running), the previous folder and files don't open. I'm not sure if there is a workaround for this.

Solution 7 - Visual Studio-Code

This will also work in case of unintended shutdown (like an OS restart for OS updates, or anything where VSCode is forcefully killed) with VSCode 1.42 (Q1 2020).

See vscode/issue 12058 ("Save UI state periodically to prevent loss on shutdown") and PR 87158 with commit 54ab792, which includes the comment:

	// In some environments we do not get enough time to persist state on shutdown.
	// In other cases, VSCode might crash, so we periodically save state to reduce
	// the chance of loosing any state.
	// The window loosing focus is a good indication that the user has stopped working
	// in that window so we pick that at a time to collect state.

bpasero adds in the issue:

> Pushed a change (#87158) that will: > > - save state every 60s >- save state when the window looses focus > > The real fix is for Electron to provide API to detect the system shutdown case on Windows (e.g. what electron/electron#15620 suggests). > > These changes will probably not help when VSCode has focus and windows restarts, though in my testing it works when any non-vscode UI is popping up (e.g. some prompt from Windows to apply updates).
I think this is the best we can do for now.


Regarding the window.reopenFolders=all mentioned in issue 15949, the last (Sept 2020) comment illustrates it is not solved yet.

Update Nov. 2020 for issue 15949 in VSCode 1.52:

> The sum of options in window.restoreWindows would then be: > >- preserve (new): all windows are restored, independent of how VSCode was started >- all: all windows are restored, but not when you start VSCode on a file or folder >- folders: only folders are restored, but not when you start VSCode on a file or folder > - one: only a single window is restored, but not when you start VSCode on a file or folder >- none: no window is ever restored

> I have pushed window.restoreWindows: preserve as new choice.
all is still the default.

See:

> ## New setting to restore previous session windows

> A new setting value preserve for the existing window.restoreWindows setting allows to enforce that every window you had opened before closing VSCode is restored when you start VSCode, even if VSCode is explicitly asked to open a specific folder or file. > > A common example is double-clicking on a file to open it in VSCode or using the terminal to open a specific file or folder.
Without window.restoreWindows: preserve in that case, VSCode will only open the file or folder as instructed, but not restore any other window.


With VSCoe 1.60 (Aug. 2021), you also have:

> ## Keep editors open instead of closing when they fail to restore

> So far, when an editor failed to restore after reload of the window or restart of the application, the editor would automatically close until an editor was found that succeeds to load. > > This behaviour can lead to frustrating results where a carefully created editor layout and arrangement is going away due to editors closing. > > With this release, an editor that was once successfully opened will not close, even when it fails to restore. The editor indicates the problem and offers a way to retry the operation: > > https://media.githubusercontent.com/media/microsoft/vscode-docs/vnext/release-notes/images/1_60/keep-editor-open.png

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphifiView Question on Stackoverflow
Solution 1 - Visual Studio-CodephifiView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeLews TherinView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeghleclView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeAdam MillerView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeMatzView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeHoward CripeView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeVonCView Answer on Stackoverflow