Skip to content

Embedded terminal in VSCode and variants

RedBearAK edited this page Apr 21, 2024 · 5 revisions

Fixing shortcuts in VSCode terminal pane

In Linux, the keymapper is unable to tell which portion of a window has the focus, only the app class and title (name) of the window. Since the way the keymapper config works in terminal apps is by treating terminals as a group and changing its modifier remaps compared to "GUI" apps, the shortcuts you would expect to work in terminals won't work the same way in a terminal view that is embedded inside a "GUI" app like Visual Studio Code or any of its variants (e.g., VSCodium, Code - OSS).

But the VSCode window knows which pane you are in, and you can remap some shortcuts with an internal VSCode method so that things work a little more the way you would expect. The remaps need to be targeted to only be active in the embedded terminal, with the terminalFocus conditional test in the when part of each remap.

Here are some that I came up with or copied from other comments. Feel free to open an issue and submit more fixes if you come up with some. This needs to be entered into the keybindings.json file that you open from the Command Palette with "open keyboard shortcuts json". (Don't choose the one that says "Default Keyboard Settings".)

Saving the changes to the file should make them immediately active in the terminal pane.

Caution

Doing this will cause these shortcuts to behave differently even when Toshy or Kinto are disabled. These remaps for the embedded terminal won't go away when the keymapper is not running, unless you open the keybindings.json file again and comment them all out and save the file. This could potentially cause some confusion unless you remember what is going on. This could also mess with using a terminal app like nano.

// Place your key bindings in this file to override the defaults
[

    //////////////////////////////////////////////////////////
    // Fixes for embedded terminal for use with Toshy/Kinto //
    //////////////////////////////////////////////////////////

    {
        // Send the Ctrl+C/SIGINT interrupt sequence in terminal with Ctrl+C
        "key": "meta+c",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0003" },
        "when": "terminalFocus"
    },
    {
        // Send the Ctrl+C/SIGINT interrupt sequence in terminal with Cmd+Dot
        "key": "ctrl+.",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0003" },
        "when": "terminalFocus"
    },
    {
        // Reverse search history with Ctrl+R
        "key": "meta+r",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0012" },
        "when": "terminalFocus"
    },
    {
        // Clear screen in terminal with Cmd+L
        "key": "ctrl+l",
        "command": "workbench.action.terminal.clear",
        "when": "terminalFocus"
    },
    {
        // Copy selected text in terminal with Cmd+C
        "key": "ctrl+c",
        "command": "workbench.action.terminal.copySelection",
        "when": "terminalFocus"
    },    
    {
        // Paste text in terminal with Cmd+V
        "key": "ctrl+v",
        "command": "workbench.action.terminal.paste",
        "when": "terminalFocus"
    },
    {
        // Delete all text left of cursor in terminal (Ctrl+U sequence)
        // For Kinto (Cmd+Backspace)
        "key": "ctrl+backspace",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0015" },
        "when": "terminalFocus"
    },
    {
        // Delete all text left of cursor in terminal (Ctrl+U sequence)
        // Alternate for Toshy (Shift+Cmd+Backspace)
        "key": "shift+ctrl+backspace",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0015" },
        "when": "terminalFocus"
    },
    {
        // Delete word left of cursor in terminal (Ctrl+W sequence)
        "key": "alt+backspace",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0017" },
        "when": "terminalFocus"
    },

]

Some shortcuts are protected and can't be changed this way. For instance, you can't remap Ctrl+B from this file, so Cmd+B will move the cursor one character to the left in the embedded terminal. If you want to instead have Cmd+B toggle the sidebar visibility the same way it does outside of the terminal pane, you have to add this to your "User Settings JSON" file:

    "terminal.integrated.commandsToSkipShell": [
        "workbench.action.toggleSidebarVisibility"
    ]

This keeps the shell from consuming the Ctrl+B key combo that is emitted by the physical equivalent of Cmd+B.