How to integrate window navigation with tmux

From Vifm Wiki
Revision as of 11:55, 31 August 2021 by Xaizek (talk | contribs) (Initial version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

You might want to use a consistent set of keys to move between panes of Vifm and tmux windows. Let's say you want to use C-hjkl. This requires configuring both tmux and Vifm.

tmux part (Vim, fzf, Vifm)

The settings are from the README of the vim-tmux-navigator plugin that in turn is "a repackaging of Mislav Marohnić's tmux-navigator configuration described in this gist."

Keegan Lowenstein expanded these to account for fzf.

Here's a version that accounts for Vifm as well (put into ~/.tmux.conf):

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

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

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

bind -n C-h run "($is_vim && tmux send-keys C-h) || \
                         ($is_vifm && tmux send-keys C-h) || \
                         tmux select-pane -L"

bind -n C-j run "($is_vim && tmux send-keys C-j)  || \
                         ($is_vifm && tmux send-keys C-j) || \
                         ($is_fzf && tmux send-keys C-j) || \
                         tmux select-pane -D"

bind -n C-k run "($is_vim && tmux send-keys C-k) || \
                         ($is_vifm && tmux send-keys C-k) || \
                         ($is_fzf && tmux send-keys C-k)  || \
                         tmux select-pane -U"

bind -n C-l run  "($is_vim && tmux send-keys C-l) || \
                         ($is_vifm && tmux send-keys C-l) || \
                         tmux select-pane -R"

bind-key -n C-\if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

Vifm part

These mappings will switch tmux window when appropriately (put into $MYVIFMRC):

nnoremap <silent> <c-h> :if paneisat('left')   | execute '!tmux select-pane -L %i' | else | execute 'wincmd h' | endif<cr>
nnoremap <silent> <c-j> :if paneisat('bottom') | execute '!tmux select-pane -D %i' | else | execute 'wincmd j' | endif<cr>
nnoremap <silent> <c-k> :if paneisat('top')    | execute '!tmux select-pane -U %i' | else | execute 'wincmd k' | endif<cr>
nnoremap <silent> <c-l> :if paneisat('right')  | execute '!tmux select-pane -R %i' | else | execute 'wincmd l' | endif<cr>

See also