Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open explorer and close sidebar with the same key

I would like to use the View: Show Explorer command when the Sidebar is closed. And close the sidebar with the same key when the Sidebar is open.

So basically I want to let my Sidebar open with the Explorer by default (and also change the focus to the explorer by default), but also close the sidebar with the same key. This can be done with the when function in the vscode keyboard shortcuts, but I dont find the function which represents when the sidebar is open.

Already tried out: workbench.SideBar.visible: true 'sideBar.isOpen || sidebarIsOpen'

where are all these when functions listed in the documentation of VSCode?

just to be clear: it is one key which is used for two different shortcut actions - one is open+focus explorer and the other is toggle sidebar (but only when its open) - so it could also be named "close sidebar".

like image 381
raphaelsmosaic Avatar asked Sep 02 '25 03:09

raphaelsmosaic


2 Answers

Try these keybindings in your keybindings.json:

  {
    "key": "ctrl+shift+e",             // when Explorer not open
    // "command": "workbench.view.explorer",   // either of these commands works
    "command": "workbench.files.action.focusFilesExplorer",
    "when": "!explorerViewletVisible"
  },
  {
    "key": "ctrl+shift+e",              // when Explorer open
    "command": "workbench.action.toggleSidebarVisibility",
    "when": "explorerViewletVisible"
  }

How did I find those context keys? See Inspect Context Keys Utility.

  1. Open View/Help/Toggle Developer Tools
  2. Go to and clear (right-click) the Console in the Developer Tools
  3. Trigger the command: Developer: Inspect Context Keys from the Command Palette and click anywhere in the vscode window
  4. In the Console, a large object will be created and output. Expand this object open so you can search in it.
  5. With focus in the Console, Ctrl/Cmd+F to open a find input. For me, this appears at the very bottom and is tricky to see.
  6. Type what you want to find, like "explorer"
  7. I found 21 hits and looked at them all to see which were promising - you can Enter through them if focus is in the find input.

explorerViewletVisible looked most promising. Then I looked through the KeyBoard Shortcuts for "view explorer" and then "sidebar" to find commands I thought would work.

like image 142
Mark Avatar answered Sep 04 '25 22:09

Mark


Here is my solution:

  {
    "key": "ctrl+k ctrl+b",
    "command": "workbench.view.explorer",
    "when": "viewContainer.workbench.view.explorer.enabled"
  },
  {
    "key": "ctrl+k ctrl+b",
    "command": "workbench.action.closeSidebar",
    "when": "sideBarVisible"
  },

Similar behavior to sublime text ctrl+k ctrl+b shortcut which toggles explorer view.

like image 34
Sahar Avatar answered Sep 04 '25 22:09

Sahar