christoomey / vim-tmux-navigator

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

Cannot navigate out of Neovim pane when opened without arguments #358

Closed stefanlogue closed 11 months ago

stefanlogue commented 11 months ago

I'm using this installed as a TPM plugin, but the same can be seen for both setup options.

The plugin works fine when I open a Neovim window with arguments, e.g. nvim . However, if I open Neovim without arguments, i.e. nvim , I cannot navigate out of the pane containing Neovim.

I've checked :TmuxNavigatorProcessList both when working and not working, and it doesn't change.

christoomey commented 11 months ago

My guess is something is overriding the key bindings when opened without an arg. Two questions:

  1. Do you see any errors, or does navigating with C-h and the other key bindings just not work for you?
  2. Can you check the output of the following in both the nvim . and nvim cases: :verbose nmap <C-h>?
stefanlogue commented 11 months ago
  1. Not seeing any errors
  2. In the case of nvim, I get: n <C-H> * <C-W>h Go to left window Last set from Lua In the case of nvim ., I get: <C-H> * :<C-U>TmuxNavigateLeft<CR> Last set from ~/.local/share/nvim/lazy/vim-tmux-navigator/plugin/tmux_navigator.vim line 19

So yes, it's being overwritten when opened without an arg. Do you know if there's a way to stop this from happening?

stefanlogue commented 11 months ago

I've managed to work around this now by adding the keymaps to my keymap.lua file manually. For future reference, this is caused by LazyVim loading it's own keymaps for those keys on startup. I'm still not 100% sure why this only happens when Neovim is opened without arguments though

christoomey commented 11 months ago

Glad you're able to sort this out, and thanks for sharing back the notes. I'm going to close this now as I don't believe the issue is in the plugin.

sdondley commented 11 months ago

@stefanlogue Can you share your keymaps.lua file with me? I'm new to lazyvim and my file is blank. I'm not sure how to add keymaps to it. Not sure what kind of data structure it is supposed to return. Thanks.

stefanlogue commented 11 months ago

@sdondley Sure thing! I have a local function in there to make mapping a bit more readable, I'll include that below:

local function map(mode, lhs, rhs, opts)
  local keys = require("lazy.core.handler").handlers.keys
  ---@cast keys LazyKeysHandler
  -- do not create the keymap if a lazy keys handler exists
  if not keys.active[keys.parse({ lhs, mode = mode }).id] then
    opts = opts or {}
    opts.silent = opts.silent ~= false
    if opts.remap and not vim.g.vscode then
      opts.remap = nil
    end
    vim.keymap.set(mode, lhs, rhs, opts)
  end
end

You'll then want to use this funtion to remap keys, here are my maps to make this plugin work correctly on all loads:

-- Vim Tmux Navigator (needed as LazyVim overwrites these when opened without args)
map("n", "<C-h>", "<Cmd>TmuxNavigateLeft<CR>", { desc = "Navigate Left" })
map("n", "<C-j>", "<Cmd>TmuxNavigateDown<CR>", { desc = "Navigate Down" })
map("n", "<C-k>", "<Cmd>TmuxNavigateUp<CR>", { desc = "Navigate Up" })
map("n", "<C-l>", "<Cmd>TmuxNavigateRight<CR>", { desc = "Navigate Right" })
map("n", "<C-\\>", "<Cmd>TmuxNavigatePrevious<CR>", { desc = "Previous Window" })
hlspablo commented 7 months ago

Guys, anyone can help me? I'm using lazy vim and did the remapping, but it simply does not work. i get this: image

when using verbose nmap but when i press the Control + h it does not change tmux window, but nvim windows.