1

I would like to delete several commands from VSCode command palette's "recently used" section, but not clear the entire history. How?


So in Chrome's Omnibar, you can use Shift+del to delete a suggestion. But I cannot find an analogous shortcut in VSCode's command palette.

I also looked for a "meta-command" for this, but I only found Clear Command History in the command palette. I want something like Edit/Manage Command History instead.


Edit: a history file that I can directly edit (analogous to ~/.bash_history for Bash) would also do.

cyqsimon
  • 2,752
  • 2
  • 17
  • 38

2 Answers2

0

Okay, so this question was upvoted today, which brought it back to my attention. I decided to bite the bullet this time and dug into the nuts and bolts of VSCode's files and found where this history is stored.

TLDR

Modifying the history is doable (obviously, because it has to be stored somewhere), but it's not very practical at all. Unfortunately it will continue to be very difficult, until VSCode implements official support (which may be never). I recommend using the following method ONLY IF you absolutely need a history entry deleted.


The method

Note:

  • I'm using Code OSS (the debranded build of VSCode) on Linux. The method should be applicable to other OSes, but the specific commands and paths will be different.
  • This method works on VSCode 1.74.2, the latest version as of 2023-01-04. It may or may not work with future versions.

0. Exit VSCode completely

Obviously.

Check with your resource/task/process manager to make sure VSCode is completely killed. If you're not sure, just reboot.

1. Locate lastSyncglobalState.json

This file contains the command palette history data. On Linux it's located at $XDG_CONFIG_HOME/Code - OSS/User/sync/globalState/lastSyncglobalState.json. On Windows it's probably under a similar path in %APPDATA%. Not sure about MacOS. Copy this file to somewhere convenient.

If you are curious how I discovered this file, I did a search of a command I recently ran using rg in $XDG_CONFIG_HOME/Code - OSS/. Note that you have to search the "programmatic name" of the command, not its display name (e.g. rust-analyzer.reload, not rust-analyzer: Restart server).

2. Extract the relevant data

If you open up lastSyncglobalState.json with a text editor directly, you'll find a Russian doll of escaped JSON. Technically you can do the modification straight from here, but I'm not eating this .

Fortunately jq makes this somewhat easier:

# This is Bash but I think it works on Windows CMD too? Not sure.
jq '.syncData.content | fromjson.storage."commandPalette.mru.cache".value | fromjson.entries' lastSyncglobalState.json > history.json

The extracted history.json should look something like this:

[
  {
    "key": "rust-analyzer.debug",
    "value": 297
  },
  {
    "key": "rust-analyzer.syntaxTree",
    "value": 298
  },
  // more entries...
]

3. Modify

Copy history.json to history-new.json, and simply remove the entries you want to delete from history-new.json. Do not modify history.json; we will need it in a bit.

Check that it's still valid JSON after your edits; in particular make sure that you have not left a trailing comma in the array.

4. Write back

The responsible way to do this is to perform the inverse of step 2, starting from the bottom up, update a field, json-encode, then update the field one level up, json-encode again, etc, until we get to the top level. But that's an enormous pain in the arse to do with jq.

Much easier I think, simply double (triple?) json-encode history-new.json, and perform a textual replacement. This is where the original history.json comes in handy:

# In lastSyncglobalState.json, replace the output of...
jq 'tojson | tojson' history.json

# with the output of...
jq 'tojson | tojson' history-new.json

Note that since the output of jq is quoted, it's necessary to remove the outmost layer of quotes (") on both the search string and the replace string. With rg we can automate this:

jq 'tojson | tojson' history.json | rg '^"(.+)"$' -r '$1'
jq 'tojson | tojson' history-new.json | rg '^"(.+)"$' -r '$1'

Of course there's nothing wrong with doing it manually, or with using sed instead if you want to. Again, just be careful you're not creating invalid JSON.

5. Copy back into VSCode directory

Honestly, you probably want to make a backup of the entire $XDG_CONFIG_HOME/Code - OSS/ directory (or whatever it is on your machine) before doing this. It's probably big I know, but I'm not sure what crazy thing VSCode will do if it finds lastSyncglobalState.json unparseable. Better be safe than sorry.

After you've done that, just copy your modified lastSyncglobalState.json back into $XDG_CONFIG_HOME/Code - OSS/User/sync/globalState/lastSyncglobalState.json and voila.

cyqsimon
  • 2,752
  • 2
  • 17
  • 38
-3

VSCode terminal uses external shell. For linux the default shell it's bash, for windows it's powershell.

If your terminal shell is powershell, go to C:\Users\john\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline on your file explorer

Visit this link for more details, if the above does not help.