christoomey / vim-tmux-navigator

Seamless navigation between tmux panes and vim splits
MIT License
5.31k stars 328 forks source link

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

Open kvngvikram opened 4 years ago

kvngvikram commented 4 years ago

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?

christoomey commented 4 years ago

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.

kvngvikram commented 4 years ago

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.

pkfm commented 3 years ago

@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
kvngvikram commented 3 years ago

@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.