From cd40be72fc1b3d25bcc53e445a21d84964300031 Mon Sep 17 00:00:00 2001 From: zoomiti Date: Sun, 28 Sep 2025 15:55:03 -0700 Subject: [PATCH] Improves nvim/tmux navigation --- .config/nvim/init.lua | 48 ++++++++++++++++++++++----- .tmux.conf | 76 ++++++++++++++++++++++++++++--------------- 2 files changed, 88 insertions(+), 36 deletions(-) diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua index c9a88eb..c652e02 100644 --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -178,7 +178,44 @@ require "pack".add({ } } }, - "christoomey/vim-tmux-navigator" + { + "alexghergh/nvim-tmux-navigation", + -- {{{ nvim-tmux-navigation config + config = function() + local nvim_tmux_nav = require('nvim-tmux-navigation') + + nvim_tmux_nav.setup { + disable_when_zoomed = true -- defaults to false + } + + vim.keymap.set('n', "", nvim_tmux_nav.NvimTmuxNavigateLastActive) + vim.keymap.set('n', "", nvim_tmux_nav.NvimTmuxNavigateNext) + + -- Custom keymaps that work with your Ctrl-w workflow + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateLeft, { desc = 'Navigate left' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateDown, { desc = 'Navigate down' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateUp, { desc = 'Navigate up' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateRight, { desc = 'Navigate right' }) + + -- Also support single letters after Ctrl-w (like standard Vim) + vim.keymap.set('n', 'h', nvim_tmux_nav.NvimTmuxNavigateLeft, { desc = 'Navigate left' }) + vim.keymap.set('n', 'j', nvim_tmux_nav.NvimTmuxNavigateDown, { desc = 'Navigate down' }) + vim.keymap.set('n', 'k', nvim_tmux_nav.NvimTmuxNavigateUp, { desc = 'Navigate up' }) + vim.keymap.set('n', 'l', nvim_tmux_nav.NvimTmuxNavigateRight, { desc = 'Navigate right' }) + + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateLeft, { desc = 'Navigate left' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateDown, { desc = 'Navigate down' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateUp, { desc = 'Navigate up' }) + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateRight, { desc = 'Navigate right' }) + + -- Previous pane/split + vim.keymap.set('n', '', nvim_tmux_nav.NvimTmuxNavigateLastActive, + { desc = 'Navigate to previous' }) + vim.keymap.set('n', 'w', nvim_tmux_nav.NvimTmuxNavigateLastActive, + { desc = 'Navigate to previous' }) + end + -- }}} + } }) vim.opt.number = true @@ -244,14 +281,7 @@ vim.keymap.set('n', 'w', 'write', { desc = "Write File" }) -- {{{ Terminal Mode vim.keymap.set('t', '', '') -vim.keymap.set('t', 'h', 'h') -vim.keymap.set('t', 'j', 'j') -vim.keymap.set('t', 'k', 'k') -vim.keymap.set('t', 'l', 'l') -vim.keymap.set('t', '', '') -vim.keymap.set('t', '', '') -vim.keymap.set('t', '', '') -vim.keymap.set('t', '', '') +vim.keymap.set('t', '', '') vim.api.nvim_create_autocmd('BufEnter', { pattern = 'term://*', command = 'startinsert' }) vim.api.nvim_create_autocmd('TermOpen', { callback = function(args) diff --git a/.tmux.conf b/.tmux.conf index aa2771b..ce26f46 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -4,6 +4,7 @@ bind r source-file ~/.tmux.conf unbind C-b set -g prefix C-s bind C-s send-prefix +bind C-w send-keys C-w set -g mouse on @@ -17,6 +18,8 @@ set -g pane-base-index 1 # Renumber windows when a window is closed set -g renumber-windows on +set -g set-clipboard on + # Status Line set-option -g status-position top set -g status-bg black @@ -27,45 +30,64 @@ set -g pane-border-style fg=brightblack set -g pane-active-border-style fg=yellow set -g message-style bg=yellow,fg=black,bold +# Background color logic +bg_color="#{?#{==:#{client_key_table},VIMLIKE},brightmagenta,#{?client_prefix,brightblue,yellow}}" + +# Text logic +status_text="#{?#{==:#{client_key_table},VIMLIKE}, WINDOW,#{?client_prefix, PREFIX, NORMAL}}" + # Left side - yellow section with session, transition to gray -set -g status-left "#[fg=black,bg=brightblue,bold]#{?client_prefix, PREFIX,#[fg=black,bg=yellow,bold] NORMAL} #[fg=black,bg=white] #S " +set -g status-left "#[fg=black,bg=${bg_color},bold]${status_text} #[fg=black,bg=white] #S " # Right side -set -g status-right "#(whoami)@#h #{?client_utf8,| utf-8,} #[fg=black,bg=white] %I:%M %p | %b %d #[fg=black,bg=brightblue]#{?client_prefix,,#[fg=black,bg=yellow,bold]} #I:#P " +set -g status-right "#(whoami)@#h #{?client_utf8,| utf-8,} #[fg=black,bg=white] %I:%M %p | %b %d #[fg=black,bg=${bg_color}] #I:#P " # Window list in the gray middle section set -g window-status-format " #I #W " -set -g window-status-current-format "#[fg=brightblue,bg=black,bold]#{?client_prefix,,#[fg=yellow,bg=black,bold]}[#I #W]" +set -g window-status-current-format "#[fg=${bg_color},bg=black,bold][#I #W]" set -g window-status-separator "" +# Smart pane switching with awareness of Vim splits +is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ + | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'" + +# Ctrl-w prefix +bind -n C-w switch-client -T VIMLIKE + +# Navigation that works with both tmux and vim +bind -T VIMLIKE C-h if-shell "$is_vim" 'send-keys C-w C-h' 'select-pane -L' +bind -T VIMLIKE C-j if-shell "$is_vim" 'send-keys C-w C-j' 'select-pane -D' +bind -T VIMLIKE C-k if-shell "$is_vim" 'send-keys C-w C-k' 'select-pane -U' +bind -T VIMLIKE C-l if-shell "$is_vim" 'send-keys C-w C-l' 'select-pane -R' +bind -T VIMLIKE h if-shell "$is_vim" 'send-keys C-w h' 'select-pane -L' +bind -T VIMLIKE j if-shell "$is_vim" 'send-keys C-w j' 'select-pane -D' +bind -T VIMLIKE k if-shell "$is_vim" 'send-keys C-w k' 'select-pane -U' +bind -T VIMLIKE l if-shell "$is_vim" 'send-keys C-w l' 'select-pane -R' +bind -T VIMLIKE Left if-shell "$is_vim" 'send-keys C-w Left' 'select-pane -L' +bind -T VIMLIKE Down if-shell "$is_vim" 'send-keys C-w Down' 'select-pane -D' +bind -T VIMLIKE Up if-shell "$is_vim" 'send-keys C-w Up' 'select-pane -U' +bind -T VIMLIKE Right if-shell "$is_vim" 'send-keys C-w Right' 'select-pane -R' + +# Window management +bind -T VIMLIKE v if-shell "$is_vim" 'send-keys C-w v' 'split-window -h -c "#{pane_current_path}"' # vertical split +bind -T VIMLIKE s if-shell "$is_vim" 'send-keys C-w s' 'split-window -v -c "#{pane_current_path}"' # horizontal split +bind -T VIMLIKE c if-shell "$is_vim" 'send-keys C-w c' 'new-window -c "#{pane_current_path}"' # new window +bind -T VIMLIKE q if-shell "$is_vim" 'send-keys C-w q' 'kill-pane' # close pane +bind -T VIMLIKE o if-shell "$is_vim" 'send-keys C-w o' 'kill-pane -a' # only this pane + +# Resizing +bind -T VIMLIKE + if-shell "$is_vim" 'send-keys C-w +' 'resize-pane -U 5' +bind -T VIMLIKE - if-shell "$is_vim" 'send-keys C-w -' 'resize-pane -D 5' +bind -T VIMLIKE < if-shell "$is_vim" 'send-keys C-w <' 'resize-pane -L 5' +bind -T VIMLIKE > if-shell "$is_vim" 'send-keys C-w >' 'resize-pane -R 5' +bind -T VIMLIKE = if-shell "$is_vim" 'send-keys C-w =' 'select-layout even-horizontal' -# Smart pane switching with awareness of Vim splits. -# See: https://github.com/christoomey/vim-tmux-navigator - -# decide whether we're in a Vim process -# is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ -# | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(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 +# Movement between windows +bind -T VIMLIKE C-w if-shell "$is_vim" 'send-keys C-w C-w' 'last-window' # Ctrl-w Ctrl-w +bind -T VIMLIKE w if-shell "$is_vim" 'send-keys C-w w' 'last-window' # Ctrl-w w # List of plugins set -g @plugin 'tmux-plugins/tpm' -set -g @plugin 'christoomey/vim-tmux-navigator' # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) run '~/.tmux/plugins/tpm/tpm'