2

In Vim I can switch between tab pages with :tabnext- to go left and tabnext+ to go right, simulating the tabs on an IDE. In Tmux I can switch between windows using <prefix>p and <prefix>n respectively. I'd like to combine these functions: I'm trying to remap the Altleft and Altright keys to switch between tab pages in Vim and session windows (which are like tabs) in Tmux when Tmux is available.

Example

https://www.codeography.com/2014/06/19/navigating-vim-and-tmux-splits The plugin 'Vim-Tmux-Navigator' provides a way to seamlessly navigate between Tmux and Vim panes via for left and right and and for up and down. (It looks like when the window number in Vim is maxed it just sends the key commands to Tmux so that there's no need for me to think about which pane in which window I want to switch to.) I'm trying to do the same thing but for switching between Vim Tab pages (created via :tabnew) and Tmux Windows (created via c) (not buffers/windows in Vim or panes in Tmux but tab pages vs Windows).

What I've tried

In ~/.vimrc I have:

if exists('$TMUX')
    function! TmuxOrTabSwitch(tabcmd, tmuxdir)                                                                      
        let previous_winnr = tabpagenr()
        silent! execute "tabnext " . a:tabcmd
        if previous_winnr == tabpagenr()                                                                         
            call system("tmux select-window -t " . a:tmuxdir)
            redraw!
        endif
    endfunction

    let previous_title = substitute(system("tmux display-message -p '#{pane_title}'"), '\n', '', '')
    let &t_ti = "\<Esc>]2;vim\<Esc>\\" . &t_ti
    let &t_te = "\<Esc>]2;". previous_title . "\<Esc>\\" . &t_te

    nnoremap <silent> <A-Left> :call TmuxOrTabSwitch('-', '-')<cr>
    nnoremap <silent> <A-Right> :call TmuxOrTabSwitch('+', '+')<cr>                                                
else
    noremap <A-Left>  :tabprevious<Cr>
    noremap <A-Right> :tabnext<Cr>
endif

In ~/.tmux.conf I have:

# Move Window left and right
bind -n M-Left run "(tmux display-message -p '#{pane_title}' | grep -iq vim && tmux send-keys M-Left) || tmux select-window -t -"
bind -n M-Right run "(tmux display-message -p '#{pane_title}' | grep -iq vim && tmux send-keys M-Right) || tmux select-window -t +"

Unfortunately, this doesn't work! How can I get this to work the way I described above? Any help would be appreciated.

  • 1
    Well for one your tmux bindings use Ctrl and not Alt – D. Ben Knoble May 30 at 16:38
  • It still doesn't work. Good catch, but when I changed it, the alt keys still don't pass anything through to Vim. They do work to change the tab in non-Tmux Vim but only change the Tmux windows in Vim+Tmux, not cycling through the tab pages in Vim. – Incalculon May 30 at 16:54
  • You're still using Ctrl in the tmux binds. – B Layer May 31 at 6:46
  • Did you confirm the .tmux.conf file? If didn't, try: tmux source-file ~/.tmux.conf – selmansamet Jun 4 at 17:14
  • I see now where I left it. I made the correction but unfortunately the problem remains. I did source the conf file again. – Incalculon Jun 6 at 21:00

Your Answer

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

Browse other questions tagged or ask your own question.