Search code examples
visual-studio-code

How can I manually edit the list of recently opened files in VS Code?


I rely heavily on the File: Open Recent… command to open frequently used files, but yesterday my local Google Drive folder got moved to a new location and now I can no longer access any of the files in that folder through the Open Recent panel because the paths don't match.

The fix would be as simple as replacing "/Google Drive/" with "/Google Drive/My Drive/" but I have no idea what file contains the list of files that appears in the recently opened panel.

I'm assuming it's somewhere in ~/Library/Application Support/Code but not sure where.


Solution

  • I was wondering the same thing the other day and found this while searching for a solution, so I took some time to investigate it today.
    It's been a a few weeks since you posted, so hopefully this will still be of help to you.

    Location of settings

    Those setting are stored in the following files:

    • Windows: %APPDATA%\Code\User\globalStorage\state.vscdb
    • MacOS: ~/Library/Application Support/Code/User/globalStorage/state.vscdb
    • Linux: ~/.config/Code/User/globalStorage/state.vscdb

    If your software is cross-platform, you may want to consider using a library such as Python's platformdirs to determine the path.

    The file is an sqlite3 database, which is used as a key-value store. It has a single table named ItemTable and the relevant key is history.recentlyOpenedPathsList.

    The value has the following structure:

    {
      "entries": [
        {
          // Only one of these two will be present, depending on whether
          // the history entry is a folder or file
          "folderUri": "path:///path/to/folder",
          "fileUri": "path:///path/to/file",
          // These properties may not exist on all entries
          "label": "...",
          "remoteAuthority": "..."
        }
      ]
    }
    

    To view the current list, you can run the following command:

    sqlite3 -readonly "$VSCODE_STATE_PATH" "SELECT [value] FROM ItemTable WHERE [key] = 'history.recentlyOpenedPathsList'" | jq
    

    Modifying the settings

    Specifically, I was interested in changing the way it's displayed (the label), so I'll detail how I did that, but it should be just as easy to update the path.

    Here's the Python code I used to make those edits:

    import json, sqlite3, platformdirs
    from typing import TypedDict, NotRequired
    
    
    class VsCodeHistoryEntry(TypedDict):
        folderUri: NotRequired[str]
        fileUri: NotRequired[str]
        label: NotRequired[str]
        remoteAuthority: NotRequired[str]
    
    
    vscode_state_path = (
        platformdirs.user_config_path() 
        / "Code/User/globalStorage/state.vscdb"
    )
    
    # Open the db, get the value and parse it
    db = sqlite3.connect(vscode_state_path)
    history_raw = db.execute("SELECT [value] FROM ItemTable WHERE  [key] = 'history.recentlyOpenedPathsList'").fetchone()[0]
    history: list[VsCodeHistoryEntry] = json.loads(history_raw)["entries"]
    
    # Make the changes you'd like
    # ...
    
    # Stringify and update
    history_raw = json.dumps({"entries": history})
    # ? used as placeholder to avoid accidental SQL injection
    db.execute(
        "UPDATE ItemTable SET [value] = ? WHERE key = 'history.recentlyOpenedPathsList'",
        (history_raw,),
    )
    db.commit()
    db.close()
    

    Code references

    For reference (mostly for my future self), here are the relevant source code areas:

    • The settings are read here.
    • The File->Open Recent uses those values as-is (see here).
    • However when using the Get Started page, the Recents area is populated here. In the Get Started, the label is presented in a slightly different way:
      vscode snapshot
    • The folder name is the link, and the parent folder is the the text beside it.
    • This is done by the splitName method.

    Notes

    • Before messing around with the settings file, it would be wise to back it up.
    • If a VS Code instance is running, the changes will not take effect, as VS Code will simply over-write the database on exit.
    • Keep in mind that there might be some state saved by other extensions, so if anything weird happens, blame it on that.
    • For reference, I'm using vscode 1.74.2 (note: editor tested and updated for v1.97.2).

    Links