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

Option / Meta key not working in Tmux

Open matthewmturner opened this issue 2 years ago • 9 comments

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?

matthewmturner avatar Oct 29 '21 04:10 matthewmturner

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

christoomey avatar Oct 29 '21 18:10 christoomey

@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 avatar Oct 29 '21 20:10 matthewmturner

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

matthewmturner avatar Oct 30 '21 03:10 matthewmturner

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

christoomey avatar Nov 01 '21 13:11 christoomey

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 avatar Nov 23 '21 13:11 leandrochiarini

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?

leandrochiarini avatar Nov 24 '21 08:11 leandrochiarini

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

Nugumanov avatar Jun 28 '22 04:06 Nugumanov

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

ksajan avatar Jan 31 '23 07:01 ksajan

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.

ksajan avatar Jan 31 '23 07:01 ksajan