てくなべ (tekunabe)

ansible / network automation / 学習メモ

VS Code でうっかり作業中のターミナルを終了しないための設定("terminal.integrated.confirmOnExit": "always")

はじめに

VS Code には統合ターミナルという機能があり、一画面の中で何かを書いたりターミナル作業したりできて便利です。私もかなりの頻度で利用します。

ただ、ワークスペースの切り替えたりするときに、うっかり作業中のターミナルを終了されてしまい「あれ、さっき実行中だったやつどうなったんだろう・・」となったり、やらかしてしまうことがありました。

先日たまたま、うっかり終了しないために、確認をとる設定("terminal.integrated.confirmOnExit": true)が分かったのでまとめます。

2022/02/26 追記

どのバージョンからかはわかりませんが(1.59?)、この設定項目が取りうる値の選択肢が変わったようです。

おそらく"terminal.integrated.confirmOnExit": "always" が狙った動作なので "always" にしています。

公式ドキュメントの Default settings から、説明文含めて引用します。

  // Controls whether to confirm when the window closes if there are active terminal sessions.
  //  - never: Never confirm.
  //  - always: Always confirm if there are terminals.
  //  - hasChildProcesses: Confirm if there are any terminals that have child processes.
  "terminal.integrated.confirmOnExit": "never",

  // Controls whether to confirm killing terminals when they have child processes. When set to editor, terminals in the editor area will be marked as changed when they have child processes. Note that child process detection may not work well for shells like Git Bash which don't run their processes as child processes of the shell.
  //  - never: Never confirm.
  //  - editor: Confirm if the terminal is in the editor.
  //  - panel: Confirm if the terminal is in the panel.
  //  - always: Confirm if the terminal is either in the editor or panel.
  "terminal.integrated.confirmOnKill": "editor",

設定

設定画面を開きます。

f:id:akira6592:20200517105001p:plain
設定画面を開く

設定を有効にしたい範囲(ユーザー/ワークスペース)を選択します。

設定項目 terminal.integrated.confirmOnExit を探してチェックを入れて有効にします。

f:id:akira6592:20200517105447p:plain
terminal.integrated.confirmOnExit を有効にする

setting,json に書く場合は、以下のようにします。(2022/02/26 true から "always" に修正)

    "terminal.integrated.confirmOnExit": "always",

以上で設定が有効になりました。VS Code の再起動は不要です。

設定確認

どのようになるか確認します。

以下のようにターミナルを開いている状態でで、ウィンドウを閉じます。

f:id:akira6592:20200517110013p:plain
ウィンドウを閉じる

すると、以下のように、確認ダイアログが表示されます。ここでキャンセルするとうっかりターミナルを終了せずにすみます。デフォルトではこの確認ダイアログが表示されずに終了します。

f:id:akira6592:20200517110137p:plain
確認ダイアログ

地味に便利なのが、閉じたけど生きてるターミナルがあるときも確認してくれる点です。

f:id:akira6592:20200517111152p:plain
助かる

おわりに

個人的にはデフォルトで有効でも良いのでは、という感覚の設定です。 もしうっかり終了してお困りの経験がある方お試しください。