Anyone know how I can use the same keys to move between Vim windows as well as Tmux (or tiling WM) panes?

For example when I use my key combination to move to the right pane within VIM, VIM should first detect if there actually is a VIM pane to the right. If not it should call a Tmux command to move to the tmux pane on the right (if a tmux pane on the right exists).

Something similar would have to happen if you move back while being in a Tmux pane.

I suppose that besides Tmux you could also use this with a tiling window manager.

Update: moving out of VIM into a Tmux pane can be done as follows:

function! WinMove(key) 
    let t:curwin = winnr()
    exec "wincmd ".a:key
    if (t:curwin == winnr()) "we haven't moved
        if (match(a:key,'[h]')) "we're we going left
            silent execute "!tmux select-pane -L &>/dev/null &" | redraw!
        elseif (match(a:key,'[j]')) "we're we going down
            silent execute "!tmux select-pane -D &>/dev/null &" | redraw!
        elseif (match(a:key,'[k]')) "we're we going up
            silent execute "!tmux select-pane -U &>/dev/null &" | redraw!
        elseif (match(a:key,'[l]')) "we're we going right
            silent execute "!tmux select-pane -R &>/dev/null &" | redraw!

map  <Esc><Up> <A-Up>
nmap <silent> <A-Right> :call WinMove('k')<CR> 

map  <Esc><Down> <A-Down>
nmap <silent> <A-Right> :call WinMove('j')<CR> 

map  <Esc><Left> <A-Left>
nmap <silent> <A-Right> :call WinMove('h')<CR> 

map  <Esc><Right> <A-Right>
nmap <silent> <A-Right> :call WinMove('l')<CR> 

Not sure yet if moving back is even possible, but I'm going to investigate it.

  • What a weird and interesting idea! – romainl Aug 24 '12 at 12:15
  • Going from the last vim window to the next tmux pane should be possible; switching to the correct vim window when you return to the vim pane (depending on which "direction" you arrived from) may be nearly impossible. – chepner Aug 25 '12 at 19:53
  • Just out of curiosity, have you managed to get this to work? – Justin L. Mar 3 '13 at 20:56
  • there is a related solution to this here: superuser.com/questions/1467829/… – Richard Wonka Aug 7 '19 at 6:28
  • There is a related question with a good solution here: superuser.com/questions/1467829/… – Richard Wonka Aug 7 '19 at 6:31

Mislav Marohnic and thoughtbot's Chris Toomey came up with a solution to this here. It uses Ctrl+h/j/k/l to switch between panes in both Vim and tmux in the way you would expect; and Ctrl+\ to switch to the previous one.

Edit: I previously suggested something I never actually tried out. If for some reason you're interested in it, see this answer's edit history.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.