Search code examples
visual-studio-codevscode-keybinding

In VS Code, how can I keybind toggling between the most recent non-terminal editor and the terminal / a terminal editor?


For a long time this has been one of my most used shortcut on VS Code:

{"key": "f1","command": "workbench.action.terminal.focus"},         
{"key": "f1","command": "workbench.action.focusActiveEditorGroup","when":"terminalFocus"}, 

A simple way to toggle back and forth between the terminal and last active editor. But out of necessity I recently decided to use editor terminals all the time, so much so, they are created by default.

But now I my trusty shortcut only works partially, it will activate a editor terminal but does not activate back to the last active file/editor. I know as far as Code is concerned an editor terminal is a editor so the command does nothing.

So I tried the following, it works if I only have two editor groups open, but If I have more than two open the wrong file is activated:

{"key": "f1","command": "workbench.action.terminal.focus"},         
{"key": "f1","command": "workbench.action.focusPreviousGroup","when":"terminalFocus"}, 

If anyone know of any clever means of toggling back and forth between a editor terminal and the last active file/editor I would really love to know!


Solution

  • Try like this in keybindings.json:

    {
        "key": "", // TODO
        "command": "workbench.action.terminal.focus",
        "when": "editorFocus",
    },
    {
        "key": "", // TODO
        "command": "workbench.action.focusActiveEditorGroup",
        "when": "terminalFocus",
    },
    {
        "key": "", // TODO
        "command": "workbench.action.navigateBack",
        "when": "terminalEditorFocus",
    },
    

    You might need to add the following to your settings.json:

    "terminal.integrated.commandsToSkipShell": [
        "workbench.action.navigateBack",
    ],