29

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?

Mikel
  • 24,855
  • 8
  • 65
  • 66
  • I found `sendText`, but it's not exported as a command. Filed https://github.com/Microsoft/vscode/issues/31262. – Mikel Jul 22 '17 at 18:17
  • Came here looking exactly for this, so thank you! But just realized that getting used to it has the potential of causing a lot of headache when working with regular terminals. Did you have problems? – marcelocra Jun 13 '20 at 13:33
  • 2
    @marcelocra I configure my terminal emulator to use these shortcuts, too. – Mikel Jun 13 '20 at 16:21

4 Answers4

32

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"
  }
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Mark
  • 143,421
  • 24
  • 428
  • 436
  • You might be interested in how I am using it to interrupt, confirm and return with just ctrl+shift+c from anywhere which is otherwise annoying. See https://stackoverflow.com/questions/46899480/vscode-terminal-terminate-process-without-prompt – Mark Oct 10 '18 at 17:59
  • This works fine for local sessions, but over the SSH plugin it doesn't seem to work. – Brent Aug 25 '20 at 16:57
  • 9
    If you’re looking for a quick way to get here, it’s the `keybindings.json` file. Easily accessed via “Preferences: Open Keyboard Shortcuts (JSON)” – Joshua Schlichting Sep 18 '20 at 20:09
19

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"
    },
]
Rav
  • 329
  • 3
  • 5
  • 1
    This should be the accepted answer because it doesn't leave anything up to interpretation and "you figure it out where to insert these lines". – blitter Apr 15 '23 at 19:33
  • This answer is way better than accepted one. – S1awek Jul 29 '23 at 07:50
4

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",
    },
Pang
  • 9,564
  • 146
  • 81
  • 122
Aalex Gabi
  • 1,525
  • 1
  • 18
  • 32
  • yo! that's great! :-) I had to follow Rav's directions where to change settings. – Daniel Katz Aug 27 '22 at 15:41
  • Excellent. May I suggest adding `"when": "terminalFocus && terminalProcessSupported"` to the `ctrl+v` example, otherwise you may paste in the terminal when attempting to paste into an editor. – totalhack Aug 29 '22 at 14:17
0

These are the bindings that work for me on OSX, I use Karabiner to map my keyboard shortcuts like a Windows machine. So the control key is command.

[
{
    "key": "cmd+shift+c",
    "command": "workbench.action.terminal.copySelection",
    "when": "terminalFocus"
},
{
    "key": "cmd+v",
    "command": "workbench.action.terminal.paste",
    "when": "terminalFocus"
},
{
    "key": "cmd+c",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
      "text": "\u0003"
    },
    "when": "terminalFocus"
},
]
Ian Smith
  • 879
  • 1
  • 12
  • 23