6

I have an existing shortcut which maximize the terminal window,

{
        "key": "ctrl+`",
        "command": "workbench.action.toggleMaximizedPanel"
}

I would like to add an additional command to the shortcut to shift the focus to the terminal window when it is maximized and back to editor window when it is minimized. is this possible in vscode?

1 Answer 1

12

In vscode v1.77 is a new command runCommands which allows you to run a sequence of commands much liek a traditional macro extension does. With the following keybindings you can do what you want with no macro extension:

{
  "key": "ctrl+`",   //  use the same keybinding below
  "command": "runCommands",
  "args": {
    "commands": [
      "workbench.action.toggleMaximizedPanel",
      "workbench.action.terminal.focus",
    ]
  },
  "when": "!terminalFocus"
},

{
  "key": "ctrl+`",   // same keybinding as above
  "command": "runCommands",
  "args": {
    "commands": [
      "workbench.action.toggleMaximizedPanel",
      "workbench.action.focusActiveEditorGroup",
    ]
  },
  "when": "terminalFocus"  // terminal will be focused when it is maximized
}

I note there is a new requirement that the Panel be center-aligned for this to work.


[Before vscode 1.77]

I think you will have to use a macro extension like multi-command to run multiple commands with one keybinding. Once you have installed multi-command, in your settings.json:

      "multiCommand.commands": [
    
      {
          "command": "multiCommand.toggleTerminalAndFocusTerminal",
    
          "sequence": [
            "workbench.action.toggleMaximizedPanel",
            "workbench.action.terminal.focus",
          ]
        },
        
        {
          "command": "multiCommand.toggleTerminalAndFocusEditor",
    
          "sequence": [
            "workbench.action.toggleMaximizedPanel",
            "workbench.action.focusActiveEditorGroup",
          ]
        }
    ],

and then these keybindings:

    {
      "key": "ctrl+`",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.toggleTerminalAndFocusTerminal" },
      "when": "!terminalFocus"
    },

    {
      "key": "ctrl+`",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.toggleTerminalAndFocusEditor" },
      "when": "terminalFocus"
    },

So the same keybinding, Ctrl-backTick will trigger one of the two commands depending on whether the terminal has focus - note the "when": "!terminalFocus" meaning when the terminal does not have focus.

Not the answer you're looking for? Browse other questions tagged or ask your own question.