Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode terminal: terminate process without prompt

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.

like image 462
Alex Avatar asked Oct 23 '17 22:10

Alex


People also ask

How do you kill a process in terminal VS code?

Remove terminal instances by hovering a tab and selecting the Trash Can button, selecting a tab item and pressing Delete, using Terminal: Kill the Active Terminal Instance command, or via the right-click context menu.

How do you stop a process from running VS code?

VS Code maintains a debug session while the program is running, and pressing the Stop button terminates the program.

How do you stop an infinite loop in VS code terminal?

Ctrl+C should break out of an infinite loop while a cell is running.


1 Answers

Looks like the prompt will be automatically replied to in vscode v1.64. See Auto-replies in Release notes:

Auto replies

The terminal is now able to automatically reply when a specific sequences of characters is received. A good example of where this is useful, which is also the only default case, is the Windows batch script message Terminate batch job (Y/N)? after hitting ctrl+c when running a batch script. This typically just ends up causing problems for the user. The terminal will now automatically reply with Y and enter (\r) which makes ctrl+c in Windows feel much better.

terminal auto-reply demo

The feature was made generically so you can setup custom replies for other thing, just be careful when doing this as you are sending text to the process automatically. For example you could use it to automatically update Oh My Zsh when prompted:

"terminal.integrated.autoReplies": {
  "[Oh My Zsh] Would you like to check for updates? [Y/n]": "Y\r"
}

If you use Clink and enable their similar feature, you can disable it in Clink or in VS Code by setting the reply to null to avoid the two features conflicting with each other:

"terminal.integrated.autoReplies": {
  "Terminate batch job (Y/N)": null
}
  • So it looks like you set the corresponding message to null to disable the automatic reply.

set default to null: looks like they are changing the default to disabled.


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 commands 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": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.terminateTerminal" },
},

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.

like image 52
Mark Avatar answered Sep 19 '22 11:09

Mark