christoomey / vim-tmux-navigator

Seamless navigation between tmux panes and vim splits
MIT License
5.38k stars 334 forks source link

Vim -> Tmux doesn't work! #387

Open alex-belost opened 6 months ago

alex-belost commented 6 months ago

Output from :TmuxNavigatorProcessList

Ss+  zsh (qterm)

Nvim build: LazyVim

Tmux config:

set -g base-index 1 
setw -g pane-base-index 1

set -s escape-time 0
set -g status-interval 0

setw -g automatic-rename on 
set -g renumber-windows on 

set -g set-titles on      
set -g focus-events on

set -g status-position top

# better splitting
bind | split-window -h -c "#{pane_current_path}"
bind _ split-window -v -c "#{pane_current_path}"

unbind r
bind r source ~/.tmux.conf \; display 'Reloaded!'

unbind g 
bind g display-popup -d '#{pane_current_path}' -w 80% -h 80% -E lazygit

bind h select-pane -L
bind l select-pane -R
bind j select-pane -D
bind k select-pane -U

set -g mouse on

set-window-option -g mode-keys vi

set -g @catppuccin_user "on"

set -g @catppuccin_flavour 'mocha'
set -g @catppuccin_status_modules_right "session date_time battery"

set -g @catppuccin_date_time_text "%d %B %Y"

# Tmux plugins manager installation https://github.com/tmux-plugins/tpm
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'catppuccin/tmux'
set -g @plugin 'tmux-plugins/tmux-battery'

set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'christoomey/vim-tmux-navigator'

# Run Tmux plugins manager
run -b '~/.tmux/plugins/tpm/tpm'

Output from ps:

 PID TTY           TIME CMD
[id] ttys000    0:31.20 zsh (qterm)
[id] ttys001    0:00.01 tmux
[id] ttys001    0:05.25 /bin/zsh --login
[id] ttys001    0:00.01 /bin/zsh --login
[id] ttys001    0:00.14 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys002    0:00.13 zsh (qterm)
[id] ttys003    0:00.34 /bin/zsh --login
[id] ttys003    0:00.01 /bin/zsh --login
[id] ttys003    0:00.00 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys003    0:00.03 nvim
[id] ttys004    0:00.10 zsh (qterm)
[id] ttys005    0:00.32 /bin/zsh --login
[id] ttys005    0:00.01 /bin/zsh --login
[id] ttys005    0:00.00 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys008    0:34.33 zsh (qterm)
[id] ttys009    0:00.59 /bin/zsh
[id] ttys009    0:00.00 /bin/zsh
[id] ttys009    0:00.17 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys009    0:00.13 nvim
[id] ttys010    0:32.05 zsh (qterm)
[id] ttys011    0:00.38 /bin/zsh
[id] ttys011    0:00.01 /bin/zsh
[id] ttys011    0:00.16 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys012    0:32.28 zsh (qterm)
[id] ttys013    0:00.39 /bin/zsh
[id] ttys013    0:00.01 /bin/zsh
[id] ttys013    0:00.15 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s
[id] ttys014    0:31.46 zsh (qterm)
[id] ttys015    0:01.29 /bin/zsh
[id] ttys015    0:00.01 /bin/zsh
[id] ttys015    0:00.17 /Users/[user_name]/.cache/gitstatus/gitstatusd-darwin-x86_64 -G v1.5.4 -s

command: tty: /dev/ttys005 command: tmux display '#{pane_tty}' -> /dev/ttys004

christoomey commented 6 months ago

A couple of questions just to confirm:

  1. You do have the vim plugin installed in addition to the tmux plugin, right?
  2. Have you checked for conflicting keymaps in vim as per these instructions?

Presuming neither of those resolve it, can you test out the following:

  1. With vim open in the first pane (pane 0 in tmux)
  2. In a split terminal pane, run ps -o state= -o comm= -t $(tmux display -p -t 0 '#{pane_tty}')
  3. Share what the output is?

I'm seeing the following output, for context.

Ss   zsh
S+   vim
ahmedsayedabdelsalam commented 5 months ago

it's not working with me too. am using nvchad

khalil-chermiti commented 5 months ago
  -- No longer needed, using tmux navigation integration 

-- navigate = {
--  { mode = "n", keymap = "<C-l>", action = "<C-w>l", desc = "pane left" },
--  { mode = "n", keymap = "<C-h>", action = "<C-w>h", desc = "pane right" },
--  { mode = "n", keymap = "<C-j>", action = "<C-w>j", desc = "pane up" },
--  { mode = "n", keymap = "<C-k>", action = "<C-w>k", desc = "pane down" },
-- },

hey @alex-belost

I had my keymaps to navigate between neovim panes, I think this made a conflict with vim-tmux-navigator if you have a similar keymaps, try removing them and check if it solves your problem

I want to mention the the above mappings didn't cause me any trouble at a certain point, this happened recently

I am using:

TriDEntApollO commented 5 months ago

it's not working with me too. am using nvchad

I am also using Nvchad and it doesnt work, the command works fine but the keybinds don't.

supinie commented 4 months ago

I'm having some similar struggles. Navigation between Vim panes works fine, as does navigation between non-Vim tmux panes; non-Vim to vim does work but Vim to non-Vim does not. I am still able to navigate out of a Vim pane into a tmux pane using <prefix>-<arrows>.

Additionally, I don't get any output from :TmuxNavigatorProcessList, but running ps -o state= -o comm= -t $(tmux display -p -t 0 '#{pane_tty}') with a Vim instance in pane 0 gives:

S zsh
S nvim

I'm using

On a NixOS 24.11 system and using home-manager 24.11 to manage my Neovim and tmux configs:

        neovim = {
            enable = true;
            vimAlias = true;
            defaultEditor = true;
            coc.enable = true;
            plugins = with pkgs.vimPlugins; [
                coc-nvim
                coc-rust-analyzer
                # coc-nix
                coc-json
                coc-go
                coc-sh
                plenary-nvim
                indentLine
                # vim-system-copy
                vim-tmux-navigator
                # vim-rainbow
                lualine-nvim
                nvim-web-devicons
                vim-commentary
                vim-oscyank
                vimtex
                telescope-nvim
                vim-multiple-cursors
                gruvbox-material
            ];
            extraConfig = ''
                set mouse=
                set guicursor=n-v-c-i:block

                set tabstop=4
                set softtabstop=4
                set shiftwidth=4
                set expandtab
                set autoindent
                syntax on
                set number relativenumber
                set shell=/usr/bin/zsh
                set laststatus=2
                set nohlsearch
                set showcmd
                set splitbelow
                set splitright

                " map <Left> <Nop>
                " map <Right> <Nop>
                " map <Up> <Nop>
                " map <Down> <Nop>

                let g:netrw_banner = 0
                let g:netrw_liststyle = 3
                let g:netrw_browse_split = 4
                let g:netrw_altv = 1
                let g:netrw_winsize = 25

                noremap <silent> <C-t> :10split <bar> :term <CR>
                tnoremap <Esc> <C-\><C-n>
                "allow coc completion on tab
                inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#confirm() : "\<C-g>u\<TAB>"

                let g:rainbow_active = 1

                let g:system_copy_enable_osc52 = 1
                " let g:oscyank_term = 'tmux'

                colorscheme gruvbox-material

                set listchars=tab:➤\ ,trail:◆ et listchars=tab:➤\ ,trail:◆

                let filetypes = ['json']
                if index(filetypes, &filetype) != -1
                    set conceallevel=0
                endif

                nnoremap <C-p> <cmd>lua require('telescope.builtin').git_files()<cr>
                nnoremap <C-g> <cmd>lua require('telescope.builtin').live_grep()<cr>
                nnoremap <C-b> <cmd>lua require('telescope.builtin').buffers()<cr>
                nnoremap <C-i> <cmd>lua require('telescope.builtin').find_files()<cr>

                " VimTex
                " Filter out some compilation warning messages from QuickFix display
                let g:vimtex_quickfix_ignore_filters = [
                      \ 'Underfull \\hbox',
                      \ 'Overfull \\hbox',
                      \ 'LaTeX Warning: .\+ float specifier changed to',
                      \ 'LaTeX hooks Warning',
                      \ 'Package siunitx Warning: Detected the "physics" package:',
                      \ 'Package hyperref Warning: Token not allowed in a PDF string',
                      \]
                let g:vimtex_view_method = 'zathura'

                let g:tmux_navigator_disable_when_zoomed = 1
            '';
            extraLuaConfig = ''
                local config = {
                    options = {
                        icons_enabled = true,
                        theme = 'gruvbox-material',
                        component_separators = "",
                        section_separators = "",
                        disabled_filetypes = {
                            statusline = {},
                            winbar = {},
                        },
                        ignore_focus = {},
                        always_divide_middle = true,
                        globalstatus = false,
                        refresh = {
                            statusline = 1000,
                            tabline = 1000,
                            winbar = 1000,
                        }
                    },
                    sections = {
                        lualine_a = {'mode'},
                        lualine_b = {'branch', 'diff', 'diagnostics'},
                        lualine_c = {'buffers'},
                        lualine_x = {'searchcount'},
                        lualine_y = {'progress'},
                        lualine_z = {'location'}
                    },
                    inactive_sections = {
                        lualine_a = {},
                        lualine_b = {},
                        lualine_c = {'filename'},
                        lualine_x = {'location'},
                        lualine_y = {},
                        lualine_z = {}
                    },
                    tabline = {},
                    winbar = {},
                    inactive_winbar = {},
                    extensions = {}
                }
                require('lualine').setup(config)

                require('telescope').setup{
                    defaults = {
                        mappings = {
                            i = {
                            ["<CR>"] = "select_default",
                            }
                        }
                    }
                }
            '';
        };

        tmux = {
            enable = true;
            plugins = with pkgs.tmuxPlugins; [
                sensible
                vim-tmux-navigator
                yank
                # tmux-power-zoom
                gruvbox
            ];
            extraConfig = ''
                unbind C-b
                set -g prefix C-Space
                bind C-Space send-prefix
                bind | split-window -h -c "#{pane_current_path}"
                bind _ split-window -v -c "#{pane_current_path}"

                set-window-option -g mode-keys vi

                bind-key -T copy-mode-vi v send-keys -X begin-selection
                bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
                bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

                # Smart pane switching with awareness of Vim splits.
                # See: https://github.com/christoomey/vim-tmux-navigator
                is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
                    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
                bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h'  'select-pane -L'
                bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
                bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k'  'select-pane -U'
                bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-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 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
                if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
                    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"

                bind-key -T copy-mode-vi 'C-h' select-pane -L
                bind-key -T copy-mode-vi 'C-j' select-pane -D
                bind-key -T copy-mode-vi 'C-k' select-pane -U
                bind-key -T copy-mode-vi 'C-l' select-pane -R
                bind-key -T copy-mode-vi 'C-\' select-pane -l
            '';
        };
TheCoconutChef commented 3 months ago

@ahmedsayedabdelsalam , @supinie

I've been tinkering with nvchad and ran into a similar issue. Doing the following worked:

--- for nvchad users, this is in the ~/.config/nvim/lua/mappings.lua file

local unmap = vim.keymap.del

--- This fixes an issue with the vim-tmux-navigator + nvchad in which the base nvchad
--- mapping were conflicting with vim-tmux-navigator ones.
unmap("n", "<c-h>")
unmap("n", "<c-j>")
unmap("n", "<c-k>")
unmap("n", "<c-l>")
map("n", "<c-h>", "<cmd>:TmuxNavigateLeft<cr>")
map("n", "<c-j>", "<cmd>:TmuxNavigateDown<cr>")
map("n", "<c-k>", "<cmd>:TmuxNavigateUp<cr>")
map("n", "<c-l>", "<cmd>:TmuxNavigateRight<cr>")
map("n", "<c-\\>", "<cmd>:TmuxNavigatePrevious<cr>")

So, for me, it simply appears it was a conflicting mapping issue.

N.B. My tmux-navigator plugin is set as lazy = false. Might this cause it to be loaded before the nvchad mapping, causing the override?

supinie commented 3 months ago

@TheCoconutChef, I just tried your method, unfortunately I don't think the root cause is the same as I get an error trying to unmap a mapping that does not exist. I did try re-doing the maps without the unmapping and with tmux_navigator_no_mappings set but the issue persists unfortunately.

DanielSRojo commented 3 months ago

@ahmedsayedabdelsalam , @supinie

I've been tinkering with nvchad and ran into a similar issue. Doing the following worked:

--- for nvchad users, this is in the ~/.config/nvim/lua/mappings.lua file

local unmap = vim.keymap.del

--- This fixes an issue with the vim-tmux-navigator + nvchad in which the base nvchad
--- mapping were conflicting with vim-tmux-navigator ones.
unmap("n", "<c-h>")
unmap("n", "<c-j>")
unmap("n", "<c-k>")
unmap("n", "<c-l>")
map("n", "<c-h>", "<cmd>:TmuxNavigateLeft<cr>")
map("n", "<c-j>", "<cmd>:TmuxNavigateDown<cr>")
map("n", "<c-k>", "<cmd>:TmuxNavigateUp<cr>")
map("n", "<c-l>", "<cmd>:TmuxNavigateRight<cr>")
map("n", "<c-\\>", "<cmd>:TmuxNavigatePrevious<cr>")

So, for me, it simply appears it was a conflicting mapping issue.

N.B. My tmux-navigator plugin is set as lazy = false. Might this cause it to be loaded before the nvchad mapping, causing the override?

This worked for me. Thanks a lot!

johnsonjo4531 commented 2 months ago

The reason it was breaking for me was that Ctrl + {h,j,k,l} is already mapped in lazyvim.

I have no idea if this is a good idea or not (EDIT I figured out a better way to do this below so check that out before you do this one), but this is what I ended up getting to work in LazyVim. It just deletes the old keymaps when they first load I believe:

  {
    "christoomey/vim-tmux-navigator",
    keys = {
      { 
        "<c-h>", 
        function () 
          vim.keymap.del("n", "<c-h>"); 
          return "<cmd><C-U>TmuxNavigateLeft<cr>" 
        end, 
        desc="Tmux Navigate Left" 
      },
      {
        "<c-j>", 
        function () 
          vim.keymap.del("n", "<c-j>"); 
          return "<cmd><C-U>TmuxNavigateDown<cr>" 
        end, 
        desc="Tmux Navigate Down"
      },
      {
        "<c-k>", 
        function () 
          vim.keymap.del("n", "<c-k>"); 
          return "<cmd><C-U>TmuxNavigateUp<cr>" 
        end, 
        desc="Tmux Navigate Up"
      },
      {
        "<c-l>", 
        function () 
          vim.keymap.del("n", "<c-l>"); 
          return "<cmd><C-U>TmuxNavigateRight<cr>" 
        end, 
        desc="Tmux Navigate Right"
      },
    },
  },
johnsonjo4531 commented 2 months ago

Ooops that last one was a little odd. I think I just need to provide the remap option to neovim instead of deleting the keymap which is much cleaner and probably the normal way people would do it in lazyvim:

  {
    "christoomey/vim-tmux-navigator",
    remap = true,
    keys = {
      { 
        "<c-h>", 
        "<cmd><C-U>TmuxNavigateLeft<cr>", 
        desc="Tmux Navigate Left" 
      },
      {
        "<c-j>", 
        "<cmd><C-U>TmuxNavigateDown<cr>", 
        desc="Tmux Navigate Down"
      },
      {
        "<c-k>", 
        "<cmd><C-U>TmuxNavigateUp<cr>", 
        desc="Tmux Navigate Up"
      },
      {
        "<c-l>", 
        "<cmd><C-U>TmuxNavigateRight<cr>", 
        desc="Tmux Navigate Right"
      },
    },
  },

Final Edit actually it appears remap isn't even an available option so I don't know why it wasn't working before, but this is my minimal config. Maybe I had an uninstalled dependency.

  {
    "christoomey/vim-tmux-navigator",
    keys = {
      {
        "<c-h>",
        "<cmd><C-U>TmuxNavigateLeft<cr>",
        desc="Tmux Navigate Left"
      },
      {
        "<c-j>",
        "<cmd><C-U>TmuxNavigateDown<cr>",
        desc="Tmux Navigate Down"
      },
      {
        "<c-k>",
        "<cmd><C-U>TmuxNavigateUp<cr>",
        desc="Tmux Navigate Up"
      },
      {
        "<c-l>",
        "<cmd><C-U>TmuxNavigateRight<cr>",
        desc="Tmux Navigate Right"
      },
    },
  },
strikeitbroke commented 1 month ago

I had this configuration on a Mac book iterm2 and it works, but on windows using wsl2, ran into the same error. I am currently using a different plugin shown below and it works:

  {
    "alexghergh/nvim-tmux-navigation",
    event = "VeryLazy",
    config = function()
      local nvim_tmux_nav = require("nvim-tmux-navigation")
      nvim_tmux_nav.setup({
        disable_when_zoomed = true,
        -- defaults to false
        keybindings = {
          left = "<C-h>",
          down = "<C-j>",
          up = "<C-k>",
          right = "<C-l>",
          last_active = "<C-\\>",
          next = "<C-Space>",
        },
      })
    end,

  },