Karabiner から Karabiner-Elements へ

Macキーバインドのカスタマイズに今まで Karabiner を使っていたけど、macOS Sierra 以降も使えるように Karabiner-Elements に移行した。今まで Karabiner で設定していたものが、ほぼほぼ Karabiner-Elements でも可能になっていたので嬉しい限り。今回はそのへんの設定周りを書いていく。

環境

設定周り

以前に書いた Karabiner で独自のカスタムな設定を作る を Karabiner-Elements に移行するだけなので、書き方さえわかればなんとかなる。公式ドキュメント にインストール方法から 細かなカスタマイズ方法 まで載っているので読もう。

Spotlight を起動したら IME をオフにする

{
  "title": "Spotlight IME off",
  "rules": [
    {
      "description": "Spotlight IME off",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "spacebar",
            "modifiers": {
              "mandatory": [
                "left_control"
              ]
            }
          },
          "to": [
            {
              "key_code": "spacebar",
              "modifiers": [
                "left_control"
              ]
            },
            {
              "key_code": "japanese_eisuu"
            }
          ],
          "conditions": [
            {
              "type": "keyboard_type_if",
              "keyboard_types": [
                "jis"
              ]
            }
          ]
        }
      ]
    }
  ]
}

Control + [ を Esc にする

{
  "title": "Ctrl+[ to Escape",
  "rules": [
    {
      "description": "Ctrl+[ to Escape",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "close_bracket",
            "modifiers": {
              "mandatory": [
                "left_control"
              ]
            }
          },
          "to": [
            {
              "key_code": "escape"
            }
          ],
          "conditions": [
            {
              "type": "keyboard_type_if",
              "keyboard_types": [
                "jis"
              ]
            },
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^com\\.googlecode\\.iterm2"
              ]
            }
          ]
        }
      ]
    }
  ]
}

iTerm2 がアクティブの場合はこの設定は無効にしている。

Control + W でひとつ手前の単語を削除

{
  "title": "Control_L+W to Option+Delete",
  "rules": [
    {
      "description": "Control_L+W to Option+Delete",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "w",
            "modifiers": {
              "mandatory": [
                "left_control"
              ]
            }
          },
          "to": [
            {
              "key_code": "delete_or_backspace",
              "modifiers": [
                "left_option"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^com\\.googlecode\\.iterm2"
              ]
            }
          ]
        }
      ]
    }
  ]
}

これも iTerm2 がアクティブの場合はこの設定は無効。

Finder 上で Control + Command + N で新規テキストファイルを作成

{
  "title": "Create new file in Finder",
  "rules": [
    {
      "description": "Create new file in Finder",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "n",
            "modifiers": {
              "mandatory": [
                "left_command",
                "left_control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "osascript -e 'tell application \"Finder\" to make new file at (the target of the front window) as alias'"
            }
          ],
          "conditions": [
            {
              "type": "keyboard_type_if",
              "keyboard_types": [
                "jis"
              ]
            },
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.finder$"
              ]
            }
          ]
        }
      ]
    }
  ]
}

英数キーのトグルで、かなモードと英数モードの切り替え

{
  "title": "Toggle IME",
  "rules": [
    {
      "description": "Toggle IME",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "japanese_eisuu"
          },
          "to": [
            {
              "key_code": "japanese_kana"
            }
          ],
          "conditions": [
            {
              "type": "input_source_if",
              "input_sources": [
                {
                  "language": "en"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

iTerm2 上の Vim でインサートモードからノーマルモードに切り替えるときに IME を OFF にする

{
  "title": "Vim IME auto off when in normal mode",
  "rules": [
    {
      "description": "Vim IME auto off when in normal mode",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "close_bracket",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "close_bracket",
              "modifiers": [
                "control"
              ]
            },
            {
              "key_code": "japanese_eisuu"
            }
          ],
          "conditions": [
            {
              "type": "keyboard_type_if",
              "keyboard_types": [
                "jis"
              ]
            },
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.googlecode\\.iterm2"
              ]
            }
          ]
        }
      ]
    }
  ]
}

その他

Karabiner では s + d を押しながら hjkl で vim ライクなカーソル移動を設定していたけど、Karabiner-Elements では 右 Command キーを押しながら hjkl でカーソル移動するようにした。

あとキーボードの速度とかはコマンドで解決した (数値はお好みで):

defaults write -g InitialKeyRepeat -int 18
defaults write -g KeyRepeat -int 2

あとは再起動すれば OK。

上記のカスタムをわざわざ作らなくても、以下のサイトで該当するものがあればそれを使うこともできる。ありがたや!
Karabiner-Elements complex_modifications rules

まとめ

基本的には BetterTouchTool はトラックパッド関連の設定をし、Karabiner-Elements ではキーボード関連の設定する、という感じにはっきり分けている。そして、それぞれは必要最低限の設定に済ませ、使いたおさないようにしている。

あと関係ないけど VimGUI の MacVim から CLI の Neovim に変えた。これも必要最低限のプラグインのみを使い、設定もできるだけガチらないようにした。大きな変更があるときにも修正を楽にしたいものだ。