vscode terminal: terminate process without prompt

2019-01-29 10:08发布

问题:

I'm used to terminating a process in the CLI by pressing Ctrl+C twice but that doesn't work in vscode's integrated terminal. It prompts for confirmation. Is there a way to use it the same way? Or even better, with 1 keypress.

回答1:

Edit: it should be possible to do without extensions if you add "workbench.action.terminal.sendSequence", to the Setting array "terminal.integrated.commandsToSkipShell"...

{
    "key": "ctrl+c",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
        "text": "\u0003Y\u000D"
    },
    "when": "terminalFocus && !terminalTextSelected"
}

[Above added by OP, requires Ctrl-C and another Ctrl-C. See comments below. Works in terminal only.]

[Answer below works in editor or terminal panel, any non-conflicting single keybinding of choice. Requires a macro extension.]


With the new command workbench.action.terminal.sendSequence added in October, 2018 v.1.28.0 I thought I might be able to get what you want working.

You will need the multi-command extension. This setting:

"multiCommand.commands": [
  {
    "command": "multiCommand.terminateTerminal",
    "interval": 1000,
    "sequence": [
      {
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "\u0003"
        }
      },
      {
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "Y\u000D"
        }
      },
    ]
  }

It would not work without the "interval" option, it could probably be much less than 1000ms as I have it here. The interval provides a pause between the two sendSequence commands (it also would not work with just one sendSequence command with all the text in one arg).

u\003 is an End of Text (Caret = ^C).

u\000D is a Return.

And some keybinding:

{
    "key": "ctrl+shift+c",
    "command": "multiCommand.terminateTerminal"
    // "when": "editorTextFocus"
},

Certain key chords will not work (like Ctrl-C Ctrl-C).

But with Ctrl-Shift-C, whether the cursor focus is in an editor, the file explorer, or the terminal it works well. Does not kill or close the terminal or affect its history, it just stops the current job there. [I haven't investigated what happens with multiple terminals open yet - which one gets the sendSequence for example.]



回答2:

Bound kill active terminal to Ctrl+C:

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

The only drawback I see – it clears the history.