Make Ctrl+C=copy and Ctrl+Shift+C=interrupt in VSCode terminal

Viewed 12314

I'd like to have Ctrl+C copy and Ctrl+Shift+C send Ctrl+C (interrupt).

I figured out the first half

{
    "key": "ctrl+c",
    "command": "workbench.action.terminal.copySelection",
    "when": "terminalFocus"
}

But how do I do the second half? Is there a command to send an arbitrary key press to the terminal?

3 Answers

With Visual Studio Code 1.28.0 there is a command, workbench.action.terminal.sendSequence, to send arbitrary keypresses to the terminal. See Send text from a keybinding to the terminal.

  {
    "key": "ctrl+shift+c",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
      "text": "\u0003"
    },
    "when": "terminalFocus"
  }

Answers above are all good. It took me long time to figure out exactly where to put these snippets. In VSCode go to File | Preferences | Keyboard Shortcuts. Switch to json text view by pressing small icon on top-right: Open Keyboard Shortcuts (JSON) and edit setting file: keybindings.json

Example keybindings.json:

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+c",
        "command": "workbench.action.terminal.copySelection",
        "when": "terminalFocus"
    },
    {
        "key": "ctrl+v",
        "command": "workbench.action.terminal.paste",
        "when": "terminalFocus"
    },
    {
        "key": "ctrl+shift+c",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "\u0003"
        },
        "when": "terminalFocus"
    },
]

You can use Ctrl+C for both if you add a condition for text being selected in terminal. This way Ctrl+C copies if text is selected and sends SIGINT if no text is selected:

    {
        "key": "ctrl+c",
        "command": "workbench.action.terminal.copySelection",
        "when": "terminalFocus && terminalProcessSupported && terminalTextSelected"
    }

If you also want to make Ctrl+V work in terminal

    {
        "key": "ctrl+v",
        "command": "workbench.action.terminal.paste",
    },
Related