🚀 OharaLumina

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

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

📅 | 📂 Category: Programming

Tired of Visual Studio Code (VS Code) launching with a graveyard of past projects? You’re not alone. Many developers find this “feature” disruptive, pulling them back into yesterday’s code when they’re ready to tackle today’s challenges. This guide will walk you through how to configure VS Code to open fresh, allowing you to focus on the task at hand and boost your coding productivity. We’ll cover settings adjustments, workspace management, and best practices to keep your VS Code startup clean and efficient.

Understanding VS Code’s Startup Behavior

VS Code’s default behavior is to restore your previous session, including opened files and folders. This can be helpful for continuity, but often becomes an obstacle when switching between different projects. Understanding why VS Code does this is the first step towards controlling it.

VS Code aims to provide a seamless experience, picking up where you left off. However, this convenience can lead to a cluttered workspace and slow startup times, especially if you regularly work with numerous files or large projects. This automatic restoration stems from settings that control how VS Code handles sessions and workspaces.

Controlling Startup with Settings

The core of managing VS Code’s startup lies within its settings. Here’s where you can fine-tune its behavior to match your workflow.

First, navigate to Settings (File > Preferences > Settings or Code > Preferences > Settings on macOS). Search for “restore previous session” and you’ll find the crucial setting: window.restoreWindows. This setting has several options: “all”, “none”, “one”, and “preserve”.

  1. “none”: This prevents VS Code from reopening any previous windows or files. This is the cleanest option for a fresh start every time.
  2. “one”: VS Code will open only the last active window from your previous session.
  3. “all”: Restores all windows and files from your last session. This is the default setting.
  4. “preserve”: Similar to “all,” but also remembers the layout of your editor groups.

Choosing “none” is generally the most effective solution for preventing unwanted file reopening. However, “one” can be useful if you consistently work within a single project.

Leveraging Workspaces for Project Management

VS Code workspaces provide a powerful way to organize projects and control which files open on startup. A workspace is essentially a collection of folders and settings specific to a project. By using workspaces, you can isolate your projects and prevent unwanted files from cluttering your environment.

To create a workspace, simply open your project folder in VS Code, then go to File > Save Workspace As. This will create a .code-workspace file. When you open this file, VS Code will load only the folders and settings associated with that workspace, giving you a clean and focused environment. This is a highly recommended practice for managing multiple projects.

  • Keeps projects separate and organized.
  • Provides a clean startup experience.

Command Line Options and Advanced Configurations

For more granular control, you can utilize command-line options. For instance, launching VS Code with the –new-window argument will always open a new, empty window, regardless of your settings. This is particularly useful for quickly opening a new instance of VS Code without any previous baggage.

You can also create different VS Code user profiles, each with its own settings and extensions. This is beneficial for separating work and personal projects or different development environments. See the official VS Code documentation for more information on profiles and command-line arguments.

Consider extensions like “Project Manager” for easier switching between projects. These extensions can streamline workflow and enhance your control over VS Code’s startup behavior.

Best Practices for a Clean VS Code Startup

Here are some best practices to maintain a clean VS Code startup:

  • Regularly close unnecessary files and editor groups.
  • Use workspaces for project management.
  • Customize your window.restoreWindows setting.
  • Explore command-line arguments for more control.

By incorporating these practices, you can transform VS Code from a source of frustration to a powerful, focused development environment.

Switching to a more controlled startup process can significantly improve your development workflow. It allows you to dive straight into the relevant context without the distraction of past projects. Experiment with the different settings and find what works best for you. A well-configured VS Code is a happy VS Code!

Learn more about efficient coding practices. FAQ
Q: Can I change the startup behavior for specific workspaces?
A: While the window.restoreWindows setting is global, using workspaces effectively isolates projects and provides a clean slate for each workspace.

Ready to reclaim your VS Code startup? Implement these techniques today and experience a more focused and efficient coding environment. Check out additional resources on VS Code workspace management and official VS Code documentation to further enhance your workflow. Consider exploring related topics like VS Code extensions for productivity and tips for optimizing VS Code performance to maximize your coding efficiency. Explore advanced settings options here.

Question & Answer :
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?

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.

🏷️ Tags: