How to prevent VS Code remembering files outside work folder?

Viewed 320

My workflow with VS Code is to always use project folders on projects. This works great, but there's one thing that's messing things up right now;

When opening just a single file from another project while working in another project, lets's say webpack.config.js, I use Windows Explorer rightclick to open that file in VS Code. However, this always opens the file in the currently open VS Code instance.

This adds the file from that other folder now to the memory of the previously opened files in the open project while it has nothing to do with that project.

When using the quick file open of VS Code now it happens a lot VS Code opens a file from that other folder, only because that outside file has been opened once while I was working on this project.

This is error prone as now it happens a lot, especially when working fast, that the wrong, for instance, package.json or webpack.config.js file gets opened, because VS Code now also suggests files that are outside this project. That's pretty frustrating and causes changes to be made in the wrong files pretty quickly.

I know we can clear the editor history and have to do it a lot now. But to me that's the other way around, doesn't solve the real problem and when not done often the issue remains.

So I want to get rid of this issue and have the following questions:

1) How can we change the 'Open with Code' rightclick menuitem in Explorer to open files always in a new instance of VS Code? 2) Is there a config settings to disable the behaviour that VS Code remembers files that are outside of the current work folder?

Thanks in advance, this thing is bothering me for quite some time now!

2 Answers

How can we change the 'Open with Code' rightclick menuitem in Explorer to open files always in a new instance of VS Code?

I assume you are a Windows user. You can edit the context menu behavior from Regedit.

Follow the below steps:

  • Press Windows + R
  • Type regedit and press OK
  • Go to Computer\HKEY_CLASSES_ROOT\*\shell\VSCode\command in regedit
  • Add -n parameter to the command like below

regedit

It will help you to open every file in a new window when you use Open with Code.

For your second question, Is there a config settings to disable the behavior that VS Code remembers files that are outside of the current work folder?

  • Open VS Code settings
  • Search window.openFilesInNewWindow
  • Make it off to on like below

vscode

With this configuration, you can't open new files in the current workspace therefore technically you prevent VS Code remember outside files. Not directly but it helps.

I don't like how "recently opened" files are shown in VS Code Quick Open (Ctrl+P). I think OP is describing this behavior...

The behavior to remember "recently opened" files causes me problems when I mistakenly open a temporary/copy of my file, i.e.

  • C:/users/temp/code.js

... instead of the true file (i.e. located inside the currently-open VS Code project)

  • C:/git/source-control/code.js

...my confusion happens because because both files are listed among the options when I use Quick Open Ctrl+P and type "code"

For me , the Quick Open list also has the answer, any "recently-open" files should have an "X" icon on the right side. You may need to hover your mouse over the file you want to remove.

Click the "X" icon to "Remove from recently open". Quick Open will always be able to find files in your current project, even if you remove them from recently-open:

VS Code "Quick Open" lists some files (names are redacted). The first files in the list are recently open files. Each recently-open-files have an "X" icon on the right, to "Remove from recently open"

Please note I don't have access to regedit as suggested in the accepted answer; my answer here does not require regedit

Related