4
4

More than 3 years have passed since last update.

【VS Code】EditorとTerminalのフォーカスをショートカットキー1つで交互に切り替える

Posted at

目的

VS Code で EditorTerminal をショートカット1つで交互に切り替えたい!
image.png

現状だとクリックで切り替えるか、クイックオープン(Ctrl/Cmd + P)で view hoge と打つなど、EditorTerminal にそれぞれ別のコマンドで移動せねばならず、少々ストレスを感じていました。

結論

結論としては、下記コードの "ctrl+j" の部分を適宜好みのショートカットに変更して keybindings.json に追加すれば実現できます!

keybindings.json
[
    {
        "key": "ctrl+j",
        "command": "workbench.action.terminal.focus",
        "when": "!terminalFocus"
    },
    {
        "key": "ctrl+j",
        "command": "workbench.action.focusActiveEditorGroup",
        "when": "terminalFocus"
    }
]

詳細な方法1

  1. コマンドパレットから Preferences: Open Keyboard Shortcuts (JSON) を開く
    1. Ctrl/Cmd + Shift + P
    2. oksj と入力し、Preferences: Open Keyboard Shortcuts (JSON) を選択
  2. 上記コードをコピペ(もしMacなら)

これで設定したショートカットキー(上の例だと Ctrl + J)で EditorTerminal が切替可能になりました:tada:

解説

以下の2つの設定を同じショートカットキーに割り当てることで1つのショートカットキーによるフォーカスの切り替えを実現しています。

  • フォーカスが Terminal にあるときは Editor にフォーカス
  • フォーカスが Terminal に無いときは Terminal にフォーカス

そのため、フォーカスがEditorでもTerminalでもない場所にあるとき、例えばサイドパネルにあるときとかもこのショートカットで Terminal に飛ぶようになります。

(VS Codeのショートカットキー編集機能が優秀すぎる...)

補足

ショートカットキーをCtrl + Jに割り当てる場合、元の Toggle Panel のショートカットがなくなります。
そのためこのショートカットを使う場合は以下のように設定を追加しておくと Ctrl + Shift + J で使えるようになるため、便利かもしれません。

keybindings.json
[
    {
        "key": "ctrl+j",
        "command": "workbench.action.terminal.focus",
        "when": "!terminalFocus"
    },
    {
        "key": "ctrl+j",
        "command": "workbench.action.focusActiveEditorGroup",
        "when": "terminalFocus"
    },
    {
        "key": "ctrl+shift+j",
        "command": "workbench.action.togglePanel"
    },
    {
        "key": "ctrl+j",
        "command": "-workbench.action.togglePanel"
    }
]

一番下の設定は元のコマンドを無効化するものです。
なくても問題なく動きますが、わかりやすさのために追加しています。


以上になります。
皆さんぜひ快適なVS Codeライフを:balloon:


  1. コマンドパレットからPreferences: Open Keyboard Shortcuts(JSONと付いていない方)を開く(Ctrl/Cmd + KCtrl/Cmd + S)ことで、GUI操作で設定することも可能 

4
4
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
4
4