christoomey / vim-tmux-navigator

Seamless navigation between tmux panes and vim splits
MIT License
5.07k stars 319 forks source link

Option / Meta key not working in Tmux #308

Open matthewmturner opened 2 years ago

matthewmturner commented 2 years ago

I am using neovim and tmux (with tmuxinator) on my M1 Macbook with iTerm2 or Warp (https://www.warp.dev/) as my terminal (i had been using Warp primarily until this issue and saw lots of references to iTerm2 which i tried but still had issues with).

I have mapped my left option key to be my meta key in my terminal. I would like to use <M-h/j/k/l> in order to navigate between neovim splits and tmux panes. Mapping my left option key to meta and using it with h/j/k/l works as expected in neovim (including neovim in tmux - i.e. i can navigate neovim splits and go from neovim to a tmux terminal pane). However, in a tmux terminal pane it either does nothing in iTerm2 or brings up alternate text (˙∆˚¬ for hjkl to be specific) in Warp instead of switching panes or switching into nvim.

Below are my configurations which were from https://github.com/christoomey/vim-tmux-navigator and i just updated them to use meta instead of ctrl:

init.vim

let g:tmux_navigator_no_mappings = 1

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>

.tmux.conf

unbind M-h
unbind M-j
unbind M-k
unbind M-l

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'
tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
    "bind-key -n 'M-\\' if-shell \"$is_vim\" 'send-keys M-\\'  'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
    "bind-key -n 'M-\\' if-shell \"$is_vim\" 'send-keys M-\\\\'  'select-pane -l'"

bind-key -T copy-mode-vi 'M-h' select-pane -L
bind-key -T copy-mode-vi 'M-j' select-pane -D
bind-key -T copy-mode-vi 'M-k' select-pane -U
bind-key -T copy-mode-vi 'M-l' select-pane -R
bind-key -T copy-mode-vi 'M-\' select-pane -l

Do I have the right configurations? If so, any insight on how I can get this working?

christoomey commented 2 years ago

Apologies but I don't have much familiarity around using option / meta for these (the few times I've dabbled in those keys for bindings in the past have been rough). I would expect this to work, and I don't see anything obviously wrong with your config, but also know this is a tricky space. Best of luck, and please do share if you sort anything out here (or if you happen to find that the plugin is in fact doing something wrong).

matthewmturner commented 2 years ago

@christoomey thank you for the quick response :) its at least good info to know that the config is ok. will let you know if i make any progress.

matthewmturner commented 2 years ago

@christoomey actually one more point. I only ended up using Meta because ctrl didnt work with ctrl-h. When I do that I get a message that says "No more marks available". The other three commands (j/k/l) work. Any idea on that?

christoomey commented 2 years ago

Sounds like you've got a conflicting mapping in Vim (the mention of "marks" makes me think it's Vim). You can check with verbose nmap <C-h> which likely will find whatever else might be trying to run for C-h

leandrochiarini commented 2 years ago

I have the same problem in Manjaro xfce, however, it seems like it is something bigger than the plugin, even if I try to remap nnoremap <silent> <M-h> :echo "Hello Word"<CR> it seems like vim does not trigger it. Even after removing everything from my .vimrc. Is it the same for you @matthewmturner? By the way, this does not seem to be an issue in the insert mode, for instance.

leandrochiarini commented 2 years ago

In my case the general solution presented in StackExchange worked.

However, your problem seem to be well known for on OS X, does this answer solve your problem?

Nugumanov commented 2 years ago

I have the same issue using iTerm2 v3.4.15 using tmux (both v2.8 and v3) on MacOS Catalina 10.15 In iTerm settings I tried to switch between Left Option key as Normal and Meta, no effect sed -n l shows nothing when pressing Option+key

ksajan commented 1 year ago

I have the same issue as @Nugumanov. Any other alternative @leandrochiarini ?

ksajan commented 1 year ago

I resolved this by changing the left Option key to Esc+ in preferences. @Nugumanov probably you can also try this if you still haven't found the solution.