oinume journal

Scratchpad of what I learned

IntelliJ IDEAでよく使うショートカットキーをVSCodeでも使えるようにする

コードを書くためのエディタとして、IntelliJ IDEAとVSCode(Cursor)を併用して使っている。そのため、この2つのエディタのショートカットキーを同じように設定したい。IntelliJ IDEA Keybindingsを過去に使ったのだが、自分が想定してないキーも変更されてしまって使いづらいと感じたので、自分でキーバインドをカスタマイズすることにした。

ちなみにここで紹介する技はもちろんIntelliJ IDEAのみではなく、GoLand, PHPStorm, PyCharm, WebStormなどでもできる。

1. IntelliJ IDEAでよく使っている機能とショートカットキーを調べる

まず、IntelliJで自分がよく使っているキーを把握するために、Help -> My ProductivityでProductivity Guideを開く。そうすると以下のような画面が出るので、Usedの列でソートして使用頻度の高い機能名とショートカットキーを調べておく。

2. VSCode側の機能名を調べる

次に、1.で調べた機能名に対するVSCodeの機能名をChatGPTなどの生成AIを使って調べる。プロンプトは以下のような感じ。

以下のIntelliJ IDEAの機能名に対するVSCodeの機能名(英語)を教えてください。
* Search Everywhere
* Go to declaration
* Go to Implementation(s)
(以下略)

www.perplexity.ai

3. VSCodeのKeybindingを変更する

コマンドパレットからOpen Keyboard Shortcutsを開き、2.で調べた機能名に合致するCommandを調べて、IntelliJ IDEAと同じショートカットキーを登録する。生成されるkeybindings.jsonはこんな感じ。このファイルはmacOSだと ~/Library/Application Support/Code/User/keybindings.jsonに保存されているので、このファイルをGitHubなどでバージョン管理しておくと良い。

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+d",
        "command": "-workbench.action.debug.run",
        "when": "debuggersAvailable && !inDebugMode && !terminalFocus"
    },
    {
        "key": "cmd+b",
        "command": "editor.action.revealDefinition",
        "when": "editorHasDefinitionProvider && editorTextFocus"
    },
    {
        "key": "f12",
        "command": "-editor.action.revealDefinition",
        "when": "editorHasDefinitionProvider && editorTextFocus"
    },
    {
        "key": "alt+cmd+b",
        "command": "editor.action.goToImplementation",
        "when": "editorHasImplementationProvider && editorTextFocus"
    },
    {
        "key": "cmd+f12",
        "command": "-editor.action.goToImplementation",
        "when": "editorHasImplementationProvider && editorTextFocus"
    },
    {
        "key": "cmd+r",
        "command": "editor.action.startFindReplaceAction",
        "when": "editorFocus || editorIsOpen"
    },
    {
        "key": "alt+cmd+f",
        "command": "-editor.action.startFindReplaceAction",
        "when": "editorFocus || editorIsOpen"
    },
    {
        "key": "shift+f6",
        "command": "editor.action.rename",
        "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
    },
    {
        "key": "f2",
        "command": "-editor.action.rename",
        "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
    },
    {
        "key": "f1",
        "command": "editor.action.showDefinitionPreviewHover"
    },
    {
        "key": "ctrl+alt+h",
        "command": "references-view.showCallHierarchy",
        "when": "editorHasCallHierarchyProvider"
    },
    {
        "key": "shift+alt+h",
        "command": "-references-view.showCallHierarchy",
        "when": "editorHasCallHierarchyProvider"
    },
    {
        "key": "alt+f7",
        "command": "references-view.findReferences",
        "when": "editorHasReferenceProvider"
    },
    {
        "key": "shift+alt+f12",
        "command": "-references-view.findReferences",
        "when": "editorHasReferenceProvider"
    },
    {
        "key": "f2",
        "command": "editor.action.marker.next",
        "when": "editorFocus"
    },
    {
        "key": "alt+f8",
        "command": "-editor.action.marker.next",
        "when": "editorFocus"
    }
]

ここまで書いて、こんな回りくどいことをしなくとも、1.で調べた機能名とショートカットキーからVSCodeのkeybindings.json(キーバインドの設定ファイル)を生成AIに生成してもらえばいいことに気づいたw