LoginSignup
2
2

More than 3 years have passed since last update.

VSCode で実行中のタスクを全て強制終了させるキーボードショートカットを設定する

Posted at

VSCode と Angular で Web アプリを開発していて、デバッグが終了する時に ng serve を止めるという事がしたかったのだけど、Windows ではいろいろ面倒で諦めて(macOS ではできた)、ならばと実行中のタスクを全て停止する方法を探したらあった。

  1. Menu -> View -> Command Pallet -> "Open Keyboard Shortcuts"
  2. keybindings.json に次のように記述

keybindings.json

[
  { 
    "key": "ctrl+alt+c",
    "command": "workbench.action.tasks.terminate",
    "args": "terminateAll" 
  }
]

ちょっと解説すると、workbench.action.tasks.terminate は、「強制終了させるタスクを選択して実行する」というコマンドのようだけど、これに terminateAll という文字列を与えると、何も聞かずに全ての実行中タスクを強制停止してくれる。
このためだけに VSCode のコードを辿った ので間違いないと思う。

キーボードショートカットはお好みで。

参考

おまけ

そしてこの workbench.action.tasks.terminate terminateAll コマンドを、タスクとして以下のように登録し、

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "ng-serve",
            "type": "shell",
            "isBackground": true,
            "command": "ng serve",
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "^$"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "^.*Angular Live Development Server.*$",
                    "endsPattern": "^.*Compiled successfully.*$"
                }
            }
        },
        {
          "label": "postdebugKill",
          "type": "process",
          "command":[
             "${command:workbench.action.tasks.terminate}",
             "terminateAll",
          ]
        }
    ]
}

launch.json で、デバッグ実行 -> 停止したときに「全てのタスクを終了」としたかった。。。

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Debug with Chrome",
            "url": "http://localhost:4200",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "ng-serve",
            "postDebugTask": "postdebugKill"
        }
    ]
}

のだけど、この状態でデバッグ停止すると「終了するタスクを選択させるプロンプト」が表示されてしまう。
terminateAll のパラメータがうまく渡せてないようだけど、よく分からない。。。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2