visual-studio-codediffrecent-file-list

How to hide all files outside the project folder from CTRL + P (Go to file..)'s recently opened in VS Code?


When I open VS Code Diff from the command line using vscode diff file1 ../other-project/file2, then press CTRL + P, the diffed file from the other project shows up in my recently opened files.

CTRL + P

Is there a way to prevent this from happening? This is confusing, especially because these files have the same names.

Is it possible to hide all files from CTRL + P that are outside of the project folder?

-- Update 02/01/2024 (Happy new year)

The issue seems to be related more to how the search.exclude settings works with Quick Open rather than the glob pattern, because even if I enter the absolute file path into the excludes, it would remove once from Quick Open on save, but get added back again once the file is re-opened


Solution

  • I'm not aware of there being a way to achieve this robustly.

    I mean, you can manually click the "x" button to make them be forgotten, but I don't think that'll satisfy you. You can also disable recent files entirely using the search.quickOpen.includeHistory setting (also a bad workaround).

    The closest thing I've seen to this potentially becoming a thing is the feature request Option to not add files to recent history for files outside of the opened folder #98428, which gained some community support, but didn't get enough in time to get added to the backlog. As an aside, the user who raised that issue ticket got the idea that it's not okay to go about soliciting support in various dev communities, which I don't think is true (and I actually suggest people to do). It should be fine for you to raise this again as a feature-request.

    The asker of the question has now raised a feature request issue ticket at Hide all files outside the project folder from CTRL + P (Go to file..)'s recently opened in VS Code #201541.


    As for workarounds, I can't think of any robust ones. The search.exclude setting applies to history in Quick Open (courtesy of #6502), but I can't get an exclude-then-include-subset hack to work like is the case for How can I prevent VS Code from changing files that do not belong to the workspace?. There's another feature-request Add a setting to exclude files/folders from quick open history #157395, but that's the same schema as search.exclude, so I doubt it'd be usable for this use-case either.

    I discovered a possible (but not very robust) workaround being inspired by Kafiger's answer post: You can make VS Code "forget" from recently opened files from elsewhere under a common ancestor directory of the workspace folder by putting a pattern like **/<common-ancestor-directory>/**/* in search.exclude (both user or workplace settings.json work). It surprised me that this works, since I'd have expected it to cause VS Code to exclude everything under the workspace folder from search as well, but for some reason it doesn't unless you use the root directory / drive root as that common ancestor directory (like /**/*. don't ask me why- I don't know, and am too lazy to go digging in the source code for this). For example, since I put all my projects under my user home directory, I would use the following:

    "search.exclude": {
        "**/home/**/*": true, // common UNIX systems
        "**/Users/**/*": true, // Windows
    },
    

    For handling files that aren't under a common ancestor directory that isn't the root directory / drive root, you can still do similarly I think: just add similar exclusion patterns for the highest ancestor directory(ies) (that isn't the root directory / drive root) of said file(s).

    Note that you might need to either reload VS Code after making that change, or toggle to false, save, and then toggle to true, and save again.

    The reason it's not robust is because if you have a subdirectory under your workspace folder named the same thing as one of those ancestor directories that you want files inside it to show up in Quick Open, they won't, and I can't figure out a way to un-exclude that.


    Note: In VS Code 1.88, you can at least skip between sections in Quick Open by using alt+up/down on Windows and Linux, and on macOS, use cmd+up/down. See related release notes. So if you want to skip past all recent files, that's an option.