3

I'm trying to make Alt + h/j/k/l switch tmux panes and vim windows. Yet for some reason my terminal (xfce4-terminal) and and Alt do not play nicely with each other. I got Alt + h/j/k/l to switch windows in vim via the following script:

nnoremap <silent> <M-h> :TmuxNavigateLeft<cr>
nnoremap <silent> <M-j> :TmuxNavigateDown<cr>
nnoremap <silent> <M-k> :TmuxNavigateUp<cr>
nnoremap <silent> <M-l> :TmuxNavigateRight<cr>
nnoremap <silent> <M-\> :TmuxNavigatePrevious<cr>

let c='a'
while c <= 'z'
  exec "set <A-".c.">=\e".c
  exec "imap \e".c." <A-".c.">"
  let c = nr2char(1+char2nr(c))
endw

To be honest, I found the latter part of the above script off the internet, and don't understand what it's doing. Now, in my tmux config I have:

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

The result is that I can switch windows in vim, but cannot switch panes in tmux. How do I fix this? I"m running Arch Linux with xfce4 as my desktop environment. This problem seems to persist in xterm, xfce4-terminal, and gnome-terminal.

1 Answer 1

3

According to :help :map-alt-keys terminals include the Alt key as an escape character when transmitting the data to the programs. As such, you should modify your mappings to the following:

nnoremap <silent> ^[h :TmuxNavigateLeft<cr>
nnoremap <silent> ^[j :TmuxNavigateDown<cr>
nnoremap <silent> ^[k :TmuxNavigateUp<cr>
nnoremap <silent> ^[l :TmuxNavigateRight<cr>
nnoremap <silent> ^[\ :TmuxNavigatePrevious<cr>

Where ^[ is inserted in Vim by pressing <C-v><Esc>.

2
  • 1
    This is the plugin I'm using. The problem is that Alt isn't being picked up by the terminal.
    – George
    Feb 11, 2016 at 15:52
  • There are settings in .inputrc that can be adjusted, like set input-meta on (or off), set output-meta on (or off), set convert-meta on (or off). I'm trying all these, without much success, so far (otherwise I wouldn't be searching here...).
    – Pierre
    Nov 25, 2023 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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