vim-tmux-navigator icon indicating copy to clipboard operation
vim-tmux-navigator copied to clipboard

Alt or Meta-hjkl work in neovim but not in vim with same config

Open kvngvikram opened this issue 4 years ago • 4 comments

I have this in my .tmux.conf file

is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n 'M-h' if-shell "$is_vim" 'send-keys M-h' 'select-pane -L'
bind-key -n 'M-j' if-shell "$is_vim" 'send-keys M-j' 'select-pane -D'
bind-key -n 'M-k' if-shell "$is_vim" 'send-keys M-k' 'select-pane -U'
bind-key -n 'M-l' if-shell "$is_vim" 'send-keys M-l' 'select-pane -R'

and in my .vimrc I have

let g:tmux_navigator_no_mappings = 1
nnoremap <M-h> :TmuxNavigateLeft<cr>
nnoremap <M-j> :TmuxNavigateDown<cr>
nnoremap <M-k> :TmuxNavigateUp<cr>
nnoremap <M-l> :TmuxNavigateRight<cr>

This works fine when I am using Neovim but doesn't work with Vim. If I change the alt/meta key with control key then both work. I am trying to have the same configs work with both vim and neovim and want to use alt key since Ctrl-h, j, l do different stuff in bash terminal. Any suggestions about how to make vim work with alt keys?

kvngvikram avatar Jul 05 '20 10:07 kvngvikram

Hi @kvngvikram, can you clarify what "doesn't work with Vim" means? Are you still able to navigate in tmux, but not vim? Can you move within vim, but not out of vim and back to tmux? Is there any error message in Vim? Does mappy anything else with the meta key in vim, e.g. if you set nnoremap <M-r> :echo "works!"<cr> and then press <M-r> combination, does vim echo "works!"? Similar question for neovim.

christoomey avatar Jul 05 '20 13:07 christoomey

I opened Vim in a terminal and not in tmux and tried nnoremap <M-r> :echo "works!"<cr>. It doesn't work. There are no error messages, no change. Instead of <M-r> if I use <C-r> or <Leader>r it works. So there is a problem with alt mapping in Vim alone and nothing to do with tmux. Whereas Neovim can work with the same <M-r> mapping.

Other than that, tmux works fine without any problem, I can move in to a vim pane. I cannot get out of the Vim pane or move inside the Vim splits when I am using <M- mapping. I don't see any errors.

So I think my issue has noting to do with this plugin. It would help if anyone can tell me why is <M- mapping not working. Thanks for your reply.

kvngvikram avatar Jul 05 '20 14:07 kvngvikram

@kvngvikram

Set the maps from an independent vim session (i.e. that isn't nested within a tmux session) and insert the literal control characters using <C-v>. See :h i_CTRL-V if you're unfamiliar. On my setup, this produces <M-h> et. al. from a tmux nested vim session, but ^[h otherwise.

tmux next-3.3
VIM - Vi IMproved 8.2 Huge Included patches: 1-716
NVIM v0.5.0-dev+1105-gfb2adadc9

pkfm avatar Mar 18 '21 14:03 pkfm

@pkfm Thanks for the suggestion. Now I tried to map using literal character. In terminal outside tmux (byobu) Vim is working with <M-j> and other keys as well. One problem is that the literal character looks like ^[j and the mapping works for <Esc>j as well. Vim looks the both as same similar to <C-j> and <CR>. Anyway when I try switching between splits outside tmux, Alt/Meta keys work as expected and also Esc as well. But when I try in tmux, only Esc mappings work, Meta/Alt mappings do not work. So no new improvement.

kvngvikram avatar Mar 20 '21 08:03 kvngvikram