LoginSignup
12
7

More than 5 years have passed since last update.

Karabiner-Elementsで、MacのTerminalでoptionとcommandを入れ替え・自由にキーバインド変更をする例

Last updated at Posted at 2017-11-22

Mac Sierraでキーボードの入力設定が変更できるKarabiner-Elementsが、待ち望んでいたアプリケーション別のキーバインド入れ替えに対応しました。
ここでは、よく使うTerminalアプリのみで、キーボード入力を入れ替える方法を紹介します。

なお、公式でいろいろなjsonサンプルがあるため、参考にするといろいろな事ができるかと思われます。
https://pqrs.org/osx/karabiner/complex_modifications/#application_specific

Karabiner-Elements インストール方法

makeするために、 boost と、 Xcode Command Line Tools を入れる必要があります。

  • boost
    • brew install boost
  • Xcode Command Line Tools
    • XCodeをApple Storeから入れた後、XCodeのメニューよりインストールが出来ます。
  • Karabiner-Elements
    • git clone https://github.com/tekezo/Karabiner-Elements.git
    • make

commandとoptionを入れ替える

Terminalのみ入れ替えをするには、 $HOME/.config/karabiner/assets/complex_modifications/terminal.json ディレクトリ下に以下のjsonファイルを置きます。

$HOME/.config/karabiner/assets/complex_modifications/terminal.json
{
  "title": "ALT <=> CMD in Terminal",
  "rules": [
    {
      "description": "Swap option and command in Terminal",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "left_option",
            "modifiers": {
              "optional": [
                "any"
              ]
            }
          },
          "to": [
            {
              "key_code": "left_command"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "Terminal"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "left_command",
            "modifiers": {
              "optional": [
                "any"
              ]
            }
          },
          "to": [
            {
              "key_code": "left_option"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "Terminal"
              ]
            }
          ]
        }
      ]
    }
  ]
}

Karabiner-Elements に反映させたあと、[Complex Modifications]->[Rules]の下にある[Add rule]をクリックし、[Swap option and command in Terminal]の[Enable]をクリックします。

スクリーンショット 2017-08-07 16.31.01.png

追加項目が表示されない場合はjsonファイルが反映されていない可能性があるので、Karabiner-Elementsを起動し直して下さい。

自由にキーバインドを登録する

Terminal アプリで誤爆が多かった、Cmd+f, Cmd+b, Cmd+d を、それぞれ、 Option+f, Option+b, Option+d に入れ替えるソースを書きました。

{
  "title": "ALT <=> CMD in Terminal",
  "rules": [
    {
      "description": "Swap command [f b d] to alt [f b d] in Terminal",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "f",
            "modifiers": {
              "mandatory": [
                "command"
              ],
              "optional": [
                "any"
              ]
            }
          },
          "to": [
            {
              "key_code": "f",
              "modifiers": [
                "left_option"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "Terminal"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "b",
            "modifiers": {
              "mandatory": [
                "command"
              ],
              "optional": [
                "any"
              ]
            }
          },
          "to": [
            {
              "key_code": "b",
              "modifiers": [
                "left_option"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "Terminal"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "d",
            "modifiers": {
              "mandatory": [
                "command"
              ],
              "optional": [
                "any"
              ]
            }
          },
          "to": [
            {
              "key_code": "d",
              "modifiers": [
                "left_option"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "Terminal"
              ]
            }
          ]
        }
      ]
    }
  ]
}
12
7
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
12
7