christoomey / vim-tmux-navigator

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

navigating between tmux panes works fine but doesn't work in vim split panes #391

Open lazylance3 opened 1 month ago

lazylance3 commented 1 month ago

I'm running Ubuntu 22.04.4 LTS via WSL2 on Windows 11

NVIM v0.9.5
tmux 3.2a

Output for :verbose nmap <C-h>

n  <C-H>       * :<C-U>TmuxNavigateLeft<CR>
        Last set from ~/.local/share/nvim/lazy/vim-tmux-navigator/plugin/tmux_navigator.vim line 19

Output for :TmuxNavigatorProcessList

S bash
S nvim-0.9.5

tmux.conf

   1   │# change prefix from Ctrl-B to Ctrl-A
   2   │ unbind C-b
   3   │ set-option -g  prefix C-a
   4   │ bind-key C-a send-prefix
   5   │
   6   │ # Enable mouse mode
   7   │ set -g mouse on
   8   │
   9   │ # Start counting pane and window number at 1
  10   │ set -g base-index 1
  11   │ setw -g pane-base-index 1
  12   │
  13   │ # Use xclip to copy and paste with the system clipboard
  14   │ bind C-c run "tmux save-buffer - | xclip -i -sel clip"
  15   │ bind C-v run "tmux set-buffer $(xclip -o -sel clip); tmux paste-buffer"
  16   │
  17   │ # remap split pane keybindings
  18   │ bind '|' split-window -h
  19   │ bind '-' split-window -v
  20   │
  21   │ # use different cursors for insert vs normal mode
  22   │ set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'
  23   │
  24   │ # Smart pane switching with awareness of Vim splits.
  25   │ # See: https://github.com/christoomey/vim-tmux-navigator
  26   │ is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
  27   │     | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
  28   │ bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h'  'select-pane -L'
  29   │ bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
  30   │ bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k'  'select-pane -U'
  31   │ bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l'  'select-pane -R'
  32   │ tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
  33   │ if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
  34   │     "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
  35   │ if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
  36   │     "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"
  37   │
  38   │ bind-key -T copy-mode-vi 'C-h' select-pane -L
  39   │ bind-key -T copy-mode-vi 'C-j' select-pane -D
  40   │ bind-key -T copy-mode-vi 'C-k' select-pane -U
  41   │ bind-key -T copy-mode-vi 'C-l' select-pane -R
  42   │ bind-key -T copy-mode-vi 'C-\' select-pane -l

I'm using lazy.nvim as my nvim plugin manager and this is the only thing I have in plugins/init.lua

   1  │ return {
   2  │   "nvim-lua/plenary.nvim",
   3  │   "christoomey/vim-tmux-navigator",
   4  │   cmd = {
   5  │     "TmuxNavigateLeft",
   6  │     "TmuxNavigateDown",
   7  │     "TmuxNavigateUp",
   8  │     "TmuxNavigateRight",
   9  │     "TmuxNavigatePrevious",
  10  │   },
  11  │   keys = {
  12  │     { "<c-h>", "<cmd><C-U>TmuxNavigateLeft<cr>" },
  13  │     { "<c-j>", "<cmd><C-U>TmuxNavigateDown<cr>" },
  14  │     { "<c-k>", "<cmd><C-U>TmuxNavigateUp<cr>" },
  15  │     { "<c-l>", "<cmd><C-U>TmuxNavigateRight<cr>" },
  16  │     { "<c-\\>", "<cmd><C-U>TmuxNavigatePrevious<cr>" },
  17  │   },
  18  │ }
lazylance3 commented 1 month ago

I opened vim outside a tmux session and the keybindings <C-h>.. (etc) now work in vim split panes. I'm assuming this is an issue in my .tmux.conf?

spenrose commented 1 month ago

I am seeing same issue.

rgeddes commented 1 month ago

Linux Mint 21 nvim 0.10.0

I had a similar issue and found this change to .tmux.conf to be helpful:

--- is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ --- | grep -iqE '^[^TXZ ]+ +(\S+\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"

+++ is_vim="ps -o args -g $$ | grep -iqE 'n?vim?x?'"

I don't know all the ins and outs of ps, but it seems to work for me.

On the other hand, is taken over by tmux and interferes with digraphs in nvim.

htlin222 commented 3 weeks ago

I am experiencing the same issue. Pane switching in Neovim works fine when I'm not in a tmux session. However, if I map <Ctrl+hjkl> to another command, such as echom 'Hello', nothing happens. It seems that tmux is intercepting the <Ctrl+hjkl> keybindings. @rgeddes's approach seems to be on the right track, but it still doesn't work for me.

HzTTT commented 2 weeks ago

I encountered the same issue. After installing the Fig plugin, the ttys sequence in my terminal got messed up, causing the #{pane_tty} parameter to be passed incorrectly.

Here is the output of the ps command:

15775 ttys004    0:00.37 zsh (figterm)
15786 ttys005    0:00.33 /bin/zsh --login
15821 ttys005    0:00.01 /bin/zsh --login
15861 ttys005    0:00.00 /bin/zsh --login
15862 ttys005    0:00.00 /bin/zsh --login
15864 ttys005    0:00.01 /Users/chh/.cache/gitstatus/gitstatusd-darwin-arm64 -G v1.5.4 -s -1
15866 ttys005    0:00.09 nvim

The value of #{pane_tty} is ttys004, but nvim is running on ttys005, causing vim's detection to fail.

Solution: Check if the output of #{pane_tty} matches the ttys where nvim is running. If not, it could be due to the influence of terminal plugins. My temporary solution is to uninstall Fig.

htlin222 commented 2 weeks ago

@HzTTT Suggestion worked. Fig is now changed to Amazon Q for command line. Uninstalling the latter solved the problem. Thank you. 🙏

HzTTT commented 2 weeks ago

@htlin222 How do you use Amazon Q for command line?😭I seem to still be having issues with it. Here is my ps output:

PID TTY           TIME CMD
79402 ttys000    0:00.15 zsh (qterm)
79411 ttys002    0:00.08 /bin/zsh --login
79475 ttys002    0:00.00 /bin/zsh --login
79476 ttys002    0:00.02 q _ inline-shell-completion --buffer ps

The first ttys000 zsh (qterm) is mistakenly recognized by #{pane_tty}, but nvim should be on ttys002.

neurosnap commented 2 weeks ago

Possibly unrelated, but I run neovim inside alpine and ran into the same issue described above. Not sure if this was a recent change or not but I needed to install procps for things to work properly. Definitely check that ps command.

HzTTT commented 2 weeks ago

@neurosnap I'm using mac.procps doesn't seem to support mac.

dbagia commented 2 weeks ago

I am using nvim inside an alpine docker container and I have the same issue. What I have observed is that this started happening recently so I guess it was caused by a recent change?

dehidehidehi commented 3 days ago

I had the same issue and found a solution.

Opening split paned in Vim and a terminal buffer -> no issues moving in and out. Doing the same but from within Tmux -> I couldn't move into the terminal buffer.

This update changed the mappings from noremap to nnoremap. https://github.com/christoomey/vim-tmux-navigator/commit/c600cf10db1bf933aab9e357158bf9b202ecf99b

Reverting the changes by adding the following config to my vimrc fixed my issue:

let g:tmux_navigator_no_mappings = 0
noremap <silent> <C-h> :<C-U>TmuxNavigateLeft<cr>
noremap <silent> <C-j> :<C-U>TmuxNavigateDown<cr>
noremap <silent> <C-k> :<C-U>TmuxNavigateUp<cr>
noremap <silent> <C-l> :<C-U>TmuxNavigateRight<cr>
noremap <silent> <C-\> :<C-U>TmuxNavigatePrevious<cr>