vscode terminal: terminate process without prompt

狂风中的少年 提交于 2019-11-27 03:45:43

问题


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:


Without extensions:

add "workbench.action.terminal.sendSequence" to the terminal.integrated.commandsToSkipShell list in your settings.json by going to File → Preferences → Settings, then searching for the string terminal.integrated.commandsToSkipShell, and then clicking "edit in settings.json".

If there is no "terminal.integrated.commandsToSkipShell" entry in your settings.json, simply add it:

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

Then you will also need to add the following key binding (File → Preferences → Keyboard Shortcuts → open keybindings.json):

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

Note that this solution requires you to use Ctrl-C twice, and only works in the Visual Studio Code terminal.

This will also change the behaviour for any tool you run in the terminal that relies on two ctrl-c instructions: as you are now sending the capital letter Y and a newline as second instruction instead, make sure whatever you're running has an alternate means of exiting available.

With extensions:

You can use the multi-command extension, by adding 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"
        }
      },
    ]
  }

The interval provides a pause between the two sendSequence commands, and both codes are required:

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

  • u\000D is a Return.

Then add a keybinding in keybindings.json:

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

Certain key combinations will not work (like the standard sequence of Ctrl-C twice), but with Ctrl-Shift-C, whether the cursor focus is in an editor, the file explorer, or the terminal it works well.

Note that this does not kill or close the terminal or affect its history, it just stops the current job there. This works with one terminal active, but I haven't checked what happens with multiple terminals open.




回答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.



来源:https://stackoverflow.com/questions/46899480/vscode-terminal-terminate-process-without-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!