理科系の勉強日記

Linux/Ubuntu/Mac/Emacs/Computer vision/Robotics

Karabiner-ElementsでVSCodeのキーバインド変更メモ

背景

VSCodeの素晴らしい拡張Awesome Emacs Keymap
marketplace.visualstudio.com
VSCode上でもEmacs likeな操作感をエンジョイしています。ありがとうございます。ただ、MacでMetaキーがoptionに割り当てられていて微妙に押しにくいのと、他のOSで開発するときにAltの位置と違っていて混乱するというので、Karabiner-Elementsをつかってそこをなんとかしました。(Metaキーを任意のキーに割り当てるやり方があれば教えていただきたいです)

方法

MacのModifier設定で⌘とoptionを入れ替えるのはあまりにも副作用があるのでやりたくありません。VSCode上で⌘をMetaキーに割り当てれば全て解決なんですが、Awesome Emacs Keymapでそれをどう設定すればいいのかわかりませんでした。ということで、Karabiner-ElementsでVSCodeのときだけoptionと⌘をスワップするようなルールを書きました。

Karabiner-Elementsを使うと最前面になっているアプリ毎にショートカットやキー変更が設定できます。最高です。素晴らしいソフトウェアをありがとうございます。
pqrs.org

手順としては以下のようにしました。

  1. .config/karabiner/assets/complex_modifications/0.jsonというファイルを作成した*1
  2. 0.jsonに内容を記入した
  3. Karabiner-ElementsのComplex modificationsでAdd ruleで該当するものをEnableにした
{
    "title": "Visual Studio Code",
    "rules": [
      {
        "description": "Swap command and option in VSCode to use Emacs Keybinds",
        "manipulators": [
         {
            "conditions": [
              {
                "bundle_identifiers": [
                  "com\\.microsoft\\.VSCode",
                  "com\\.microsoft\\.VSCodeInsiders"
                ],
                "type": "frontmost_application_if"
              }
            ],
            "from": {
              "key_code": "left_command",
              "modifiers": {
                "optional": [
                  "any"
                ]
              }
            },
            "to": [
              {
                "key_code": "left_option"
              }
            ],
            "type": "basic"
          },
          {
            "conditions": [
              {
                "bundle_identifiers": [
                  "com\\.microsoft\\.VSCode",
                  "com\\.microsoft\\.VSCodeInsiders"
                ],
                "type": "frontmost_application_if"
              }
            ],
            "from": {
              "key_code": "left_option",
              "modifiers": {
                "optional": [
                  "any"
                ]
              }
            },
            "to": [
              {
                "key_code": "left_command"
              }
            ],
            "type": "basic"
          }
        ]

しかしこれだと既存の⌘+tabなど⌘を利用するショートカットもoptionで利用することになり少し不便です。なので、よく使うショートカットについて、⌘とoptionをさらにスワップしてみます。0.json内の"rules"が配列なので、新しい辞書を以下のように追加しました。

    {
        "description": "Command + Tab --> Option + Tab to use App changer in VSCode",
        "manipulators": [
          {
            "conditions": [
            {
              "bundle_identifiers": [
                "com\\.microsoft\\.VSCode",
                "com\\.microsoft\\.VSCodeInsiders"
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
              "key_code": "tab",
              "modifiers": { 
                "mandatory": [ 
                  "left_option"
                ] 
              }
          },
          "to": [
            {
              "key_code": "tab",
              "modifiers":
                [ 
                  "left_command" 
                ]            
            }
          ],
          "type": "basic"
          }
        ]
      }

f:id:kenbell1988:20200221102300p:plain
Add rulesすると, 自作したjsonファイルに書いた説明が表示された!個人的に使っているショートカットもついでに修正しておいた。

追記

⌘+Tab+Shiftで逆順に巡るやつの対応ができていなかった。これまでと同じようにやってもうまく機能しなかった、辛い。

*1:名前は数字でないとダメ、という情報を鵜呑みにしています