13 寸的屏幕有点小,所以 tmux 一般是多用 window 少用 pane,至少一个放 neovim 一个跑 zsh,来回切 window 多了觉得 <prefix> l
或者 <prefix> h
这样的组合键还是有点繁琐。之前一直是用 vim-tmux-navigator 做 tmux pane 之间的切换,于是就想着能不能把 <C-l>
也绑到 tmux window 切换上去。由于 vim-tmux-navigator 不支持 tmux window,只能自己搞了个配置,分享给有类似需求的朋友看下。
逻辑比较简单,以 <C-l>
为例:
<C-l>
切到 nvim 右边 window<C-l>
一下切到右边的 tmux pane<C-l>
一下切到下一个 tmux windowvim conf:
" ~/.vimrc
function! TmuxWinJump(direction)
let direction_edges = {
\ 'h': 'pane_at_left',
\ 'j': 'pane_at_bottom',
\ 'k': 'pane_at_top',
\ 'l': 'pane_at_right'
\}
let edge_cmd = 'tmux display-message -p "#{'.direction_edges[a:direction].'}"'
if trim(system(edge_cmd)) == 1
let tmux_direction = { 'h': '-', 'j': '+', 'k': '-', 'l': '+' }
call system('tmux select-window -t :'.tmux_direction[a:direction])
else
let pane_direction = tr(a:direction, 'hjkl', 'LDUR')
call system('tmux select-pane -'.pane_direction)
endif
endfunction
function! VimWinJump(direction)
let nr = winnr()
exec 'wincmd '.a:direction
if winnr() == nr | call TmuxWinJump(a:direction) | endif
endfunction
nnoremap <silent> <C-h> :call VimWinJump('h')<cr>
nnoremap <silent> <C-j> :call VimWinJump('j')<cr>
nnoremap <silent> <C-k> :call VimWinJump('k')<cr>
nnoremap <silent> <C-l> :call VimWinJump('l')<cr>
tmux conf:
~/.tmux.conf
is_nvim='tmux list-panes -F "#{l:#{pane_current_command}} #{l:#{pane_id}}" | grep #{pane_id} | grep -iqE "nvim|fzf"'
h_handler='if -F "#{pane_at_left}" "select-window -t :-" "select-pane -L"'
bind-key -n 'C-h' if-shell "$is_nvim" 'send-keys C-h' "$h_handler"
j_handler='if -F "#{pane_at_bottom}" "select-window -t :+" "select-pane -D"'
bind-key -n 'C-j' if-shell "$is_nvim" 'send-keys C-j' "$j_handler"
k_handler='if -F "#{pane_at_top}" "select-window -t :-" "select-pane -U"'
bind-key -n 'C-k' if-shell "$is_nvim" 'send-keys C-k' "$k_handler"
l_handler='if -F "#{pane_at_right}" "select-window -t :+" "select-pane -R"'
bind-key -n 'C-l' if-shell "$is_nvim" 'send-keys C-l' "$l_handler"
Inspired by https://github.com/christoomey/vim-tmux-navigator