From a6b32504347ea0e1bdddbb529b700f30563d4263 Mon Sep 17 00:00:00 2001 From: Martin Pander Date: Sun, 15 Feb 2026 20:23:16 +0100 Subject: [PATCH] Consolidate tmux and vim navigation --- modules/home/nvim.nix | 1 + modules/home/tmux.nix | 12 ++++++++---- modules/nvim/lua/keymaps.lua | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/modules/home/nvim.nix b/modules/home/nvim.nix index d7cc322..c7b03ac 100644 --- a/modules/home/nvim.nix +++ b/modules/home/nvim.nix @@ -60,6 +60,7 @@ in copilot-cmp CopilotChat-nvim opencode-nvim + vim-tmux-navigator bullets-vim nvim-dap nvim-nio diff --git a/modules/home/tmux.nix b/modules/home/tmux.nix index 65211d1..6dea275 100644 --- a/modules/home/tmux.nix +++ b/modules/home/tmux.nix @@ -23,11 +23,15 @@ in unbind S bind S command-prompt "switch -t %1" - bind-key -n M-K if -F "#{pane_at_top}" "switch-client -p" "select-pane -U" - bind-key -n M-J if -F "#{pane_at_bottom}" "switch-client -n" "select-pane -D" + # Check if we are in vim + is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ + | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'" - bind-key -n M-L if -F "#{pane_at_right}" "next-window" "select-pane -R" - bind-key -n M-H if -F "#{pane_at_left}" "previous-window" "select-pane -L" + bind-key -n M-K if-shell "$is_vim" "send-keys M-K" "if -F \"#{pane_at_top}\" \"switch-client -p\" \"select-pane -U\"" + bind-key -n M-J if-shell "$is_vim" "send-keys M-J" "if -F \"#{pane_at_bottom}\" \"switch-client -n\" \"select-pane -D\"" + + bind-key -n M-L if-shell "$is_vim" "send-keys M-L" "if -F \"#{pane_at_right}\" \"next-window\" \"select-pane -R\"" + bind-key -n M-H if-shell "$is_vim" "send-keys M-H" "if -F \"#{pane_at_left}\" \"previous-window\" \"select-pane -L\"" bind '"' split-window -c "#{pane_current_path}" bind % split-window -h -c "#{pane_current_path}" diff --git a/modules/nvim/lua/keymaps.lua b/modules/nvim/lua/keymaps.lua index eb0101a..c0766dc 100644 --- a/modules/nvim/lua/keymaps.lua +++ b/modules/nvim/lua/keymaps.lua @@ -129,3 +129,41 @@ vim.keymap.set('n', "", function() require("dap").step_into() end) vim.keymap.set('n', "", function() require("dap").step_over() end) vim.keymap.set('n', "", function() require("dap").step_out() end) + +-- Tmux Navigator +vim.g.tmux_navigator_no_mappings = 1 + +local function tmux_navigate(direction) + local old_win = vim.api.nvim_get_current_win() + vim.cmd('wincmd ' .. direction) + local new_win = vim.api.nvim_get_current_win() + + if old_win == new_win then + -- We are at the edge, let tmux handle it + -- This requires the tmux config to be set up to handle these keys when not in vim, + -- BUT since we are IN vim, we need to explicitly trigger the tmux action. + -- However, simply sending the key to tmux might just send it back to vim if we aren't careful, + -- or we can just run the tmux command directly. + + local tmux_cmd = "" + if direction == 'h' then + tmux_cmd = 'if -F "#{pane_at_left}" "previous-window" "select-pane -L"' + elseif direction == 'j' then + tmux_cmd = 'if -F "#{pane_at_bottom}" "switch-client -n" "select-pane -D"' + elseif direction == 'k' then + tmux_cmd = 'if -F "#{pane_at_top}" "switch-client -p" "select-pane -U"' + elseif direction == 'l' then + tmux_cmd = 'if -F "#{pane_at_right}" "next-window" "select-pane -R"' + end + + -- We use vim.fn.system to execute the tmux command + -- We need to wrap the command in 'tmux' call + vim.fn.system('tmux ' .. tmux_cmd) + end +end + +vim.keymap.set('n', '', function() tmux_navigate('h') end) +vim.keymap.set('n', '', function() tmux_navigate('j') end) +vim.keymap.set('n', '', function() tmux_navigate('k') end) +vim.keymap.set('n', '', function() tmux_navigate('l') end) +