全力で怠けたい

怠けるために全力を尽くしたいブログ。

Mac で Shift+Command+ ], [ の同時押しで tmux のウィンドウ間を移動する方法。

はじめに

tmux を使い始めたばかりだが、とても便利に使っている。

しかしながら、tmux のウィンドウ間を移動するデフォルトのキーバインドは、プレフィックスキーを押した後に Ctrl + n / Ctrl + p を押すというもので、これが少々面倒くさいと感じていた。 自分の場合、tmux のウィンドウ間の移動はプレフィックスキーを押した後にウィンドウ番号を押す、というキー操作を使うことが多いのだが、1つ次のウィンドウ / 1つ前のウィンドウへの移動は、プレフィックスキーを押すことなくサッと移動したいためだ。

そこで、Safari や多くのアプリケーションで採用されている、Shift+Command+ ], [ の同時押しで tmux のウィンドウ間を移動するようにしたところ、非常に捗るようになった。 このやり方をメモしておく。

tmux のバージョン。

$ tmux -V
tmux 3.3a

macOS のバージョン。

$ sw_vers | grep Product
ProductName:            macOS
ProductVersion:         13.5.1

やり方

やり方はごくシンプルで、ターミナルソフトへの Shift+Command+ ], [ キーの入力を、 tmux への プレフィックスキー & Ctrl + n / Ctrl + p に置き換えてしまう、というもの。 キー入力の置き換えには Karabiner-Elements を使う。

具体的には、次の変換ルールを Karabiner-Elements に追加するだけでよい。 自分は iTerm2 を使っているが、bundle_identifiers のところをいじれば、他のターミナルソフトでも同じことができると思う。

ただし、このやり方は Shift+Command+ ], [ の入力を他のキー入力に置き換えてしまうので、同ショートカットキーでの iTerm2 のタブ移動はできなくなってしまう。 しかしながら、個人的にはターミナルソフトを使うときはほとんど tmux を使っているので、ほとんど困ったことはない。

{
  "title": "change shift + command + square-bracket to ctrl + square-bracket",
  "rules": [
    {
      "description": "change shift + command + [ to ctrl + g and p",
      "manipulators": [
        {
          "type": "basic",
          "description": "change shift + command + [ to ctrl + g and p if iterm2",
          "from": {
            "key_code": "close_bracket",
              "modifiers": {
                "mandatory": [
                  "left_command",
                  "left_shift"
                ],
                "optional": ["any"]
              }
          },
          "to": [
            {
              "key_code": "g",
              "modifiers": [
                "left_control"
              ]
            },
            {
              "key_code": "p",
              "modifiers": []
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.googlecode\\.iterm2$"
              ]
            }
          ]
        }
      ]
    },
    {
      "description": "change shift + command + ] to ctrl + g and n",
      "manipulators": [
        {
          "type": "basic",
          "description": "change shift + command + ] to ctrl + g and n if iterm2",
          "from": {
            "key_code": "backslash",
            "modifiers": {
              "mandatory": [
                "left_command",
                "left_shift"
              ],
              "optional": ["any"]
            }
          },
          "to": [
            {
              "key_code": "g",
              "modifiers": [
                "left_control"
              ]
            },
            {
              "key_code": "n",
              "modifiers": []
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.googlecode\\.iterm2$"
              ]
            }
          ]
        }
      ]
    }
  ]
}

参考サイト