skip to Main Content

I’m trying to create a custom command in VS Code to hide/show the side bar and the activity bar at the same time, while also passing the focus to the activity bar if its shown or back to the text editor if it’s hidden. Is there an available context key to achieve that?

At keybindings.json:

{
    "key": "ctrl+b",
    "command": "runCommands",
    "args": {
        "commands": [
            "workbench.action.toggleSidebarVisibility",
            "workbench.action.toggleActivityBarVisibility",
            // {    
            //     "command": "workbench.action.focusActivityBar",
            //     "when": "sideBarVisible" //does not work
            // }
        ]
    }
}

I also tried to manipulate the focus with command "workbench.action.nextSideBarView".

2

Answers


  1. If you want to use a when clause in an individual command in runCommands, you want feature-request Allow separate when clauses for each command in a keybinding with multiple commands #184288. I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

    For now, you’ll just need to split out your command into separate keybindings with top-level when clauses (it will mean repeating yourself more, but that’s just how it’ll have be right now I think).

    Login or Signup to reply.
  2. It is pretty easy to construct a toggle for what you want to do. Try these keybindings:

    {
      "key": "ctrl+b",                // whatever you want 
      "command": "runCommands",
      "args": {
          "commands": [
              "workbench.action.maximizeEditorHideSidebar"
              // workbench.action.minimizeOtherEditorsHideSidebar  // or this
          ]
      },
      "when": "sideBarVisible"
    },
    
    {
      "key": "ctrl+b",     // use the same keybinding here to create a toggle
      "command": "runCommands",
      "args": {
          "commands": [
              "workbench.action.toggleSidebarVisibility",
              "workbench.action.focusActivityBar",
          ]
      },
      "when": "!sideBarVisible"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search