このエントリーは、Visual Studio Code Advent Calendar 2020 の24日目エントリーです。
macOSのVSCodeのターミナルでは、VSCodeはCommandキー中心のショートカットの体系となっているため、Ctrlキー主体のターミナルのキーバインドは基本的に重複しません。
しかしWindowsのVSCodeではVSCodeのショートカットは、Ctrlキーを中心に使用します。このため、WindowsのVSCodeからLinuxサーバーにSSHでリモート接続する 場合は、リモート接続したターミナル上でVSCodeのキーバインドが優先されてしまいます。
| キー入力 | ターミナル上でのキーバインド | VSCode上のキーバインド |
|---|---|---|
| Ctrl+ p | previous-history | Go to file |
| Ctrl+ e | end-of-line | Go to file |
| Ctrl+ n | next-history | New File |
| Ctrl+ k | kill-line | Open Folder(Ctrl+k Ctrl+o)等のシーケンスで使用 |
| Ctrl+ f | forward-char | Find |
この問題に対応するには、VSCodeの設定ファイルであるkeybindings.jsonに次のような設定を記述します。
{
"key": "(重複するショートカット)",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
}
keybindings.jsonを設定するには、次の手順を踏みます。
コマンドパレットから「Open Keyboard Shortcuts(JSON)」を選択する
keybindings.json上で Ctrl + Kを二回入力する設定するキーバインドを入力すると、
keybindings.json上に設定の雛形が表示されるcommand:に"terminal.integrated.commandsToSkipShell"を入力するwhen:に"terminalFocus"を入力する
設定を行ったkeybindings.jsonの例を次に示します。
[{
"key": "ctrl+e",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
},
{
"key": "ctrl+p",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
},{
"key": "ctrl+n",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
},{
"key": "ctrl+k",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
},{
"key": "ctrl+f",
"command": "terminal.integrated.commandsToSkipShell",
"when": "terminalFocus"
}
]
この設定を行うと、ターミナル上では、シェルのキーバインドが優先され、エディターグループやサイドバー等の上では、VSCode自体のキーバインドが有効になるようになります。