0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Tmux, Vim, LazygitのC-j,C-kのキーマップがコンフリクトする問題の解消

Posted at

はじめに

tmux,lazygit,vimはそれぞれCtrl-j,Ctrl-kを使います。

  • tmux: ペインの上下移動
  • vim: ウィンドウの上下移動
  • lazygit: commitの上下移動

これらを解決するまでの流れと、過程で生じた問題とその解決策を紹介します。

最初の Vim と Tmux の設定

まず、Vim と tmux のペイン移動を統一するため、以下の設定を追加しました。
vim側ではプラグイン(alexghergh/nvim-tmux-navigation)を使用しています。
こちらの設定も上記のプラグインのレポジトリのREADMEから拾ってきました。

is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"

bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h'  'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k'  'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l'  'select-pane -R'

tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\'  'select-pane -l'"

この設定により、Vimtmux両方で C-h, C-j, C-k, C-l をウィンドウ、ペイン移動に使用できるようになりました。

Lazygit のキーバインド競合

その後、lazygittmux 内で使用するようになったところ、
リベース時に C-j, C-k を用いたコミットの移動が機能しない問題が発生しました。
これは、tmuxC-j, C-k をペイン移動に割り当てていたためです。

この問題を解決するため、lazygit を検出して適切なキーを送信するようにしました。

is_lg="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\/)?lazygit$'"

bind -n C-j if "$is_lg" "send-keys C-j"  "select-pane -D"
bind -n C-k if "$is_lg" "send-keys C-k"  "select-pane -U"

参考: https://github.com/jesseduffield/lazygit/discussions/3201#discussioncomment-8037078

これにより、tmux 内でも lazygit のコミット移動が機能するようになりました。

Vim のウィンドウの上下の移動が機能しなくなる問題

しかし、この設定の影響で、Vim のウィンドウの上下移動 (C-j, C-k) が機能しなくなってしまいました。

そこで、lazygitvim の両方の状況を考慮し、すべての動作を両立させるための設定を作成しました。

bind -n C-j if-shell "$is_lg" "send-keys C-j"  "select-pane -D" \
  \; if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
bind -n C-k if-shell "$is_lg" "send-keys C-k"  "select-pane -U" \
  \; if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'

この設定により、tmux,lazygit,vimすべてでC-j,C-kが期待通りの動作をするようになりました。

まとめ

この問題を通じて、tmuxif-shell を適切に活用し、
lazygit, vim, tmux のキーマップ競合を解決する方法を学びました。

現在の設定では、

  • tmux 内でも lazygit のキー操作が可能
  • vim 内でのスプリット移動も動作
  • tmux のペイン移動も維持

と、すべての動作が期待通りに動くようになっています。

もし tmux, lazygit, vim を同時に使用していて、キーバインドの競合に悩んでいる方がいれば、ぜひこの方法を試してみてください!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?