From 701bd244be7e444928b59388099059257906cd92 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Fri, 25 Mar 2022 18:02:38 +0530 Subject: [PATCH] Allow seamless navigation between vim and tmux splits --- .gitmodules | 3 +++ nvim/.config/nvim/init.lua | 5 +++++ nvim/.config/nvim/lua/keymappings.lua | 13 ++++++++++--- nvim/.config/nvim/lua/plugins.lua | 1 + tmux/.tmux.conf | 25 ++++++++++++++++++++----- tmux/.tmux/plugins/vim-tmux-navigator | 1 + 6 files changed, 40 insertions(+), 8 deletions(-) create mode 160000 tmux/.tmux/plugins/vim-tmux-navigator diff --git a/.gitmodules b/.gitmodules index 59b2e18..1bf7c59 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "tmux/.tmux/plugins/tmux-cmd-capture"] path = tmux/.tmux/plugins/tmux-cmd-capture url = https://github.com/artemave/tmux_capture_last_command_output.git +[submodule "tmux/.tmux/plugins/vim-tmux-navigator"] + path = tmux/.tmux/plugins/vim-tmux-navigator + url = https://github.com/christoomey/vim-tmux-navigator.git diff --git a/nvim/.config/nvim/init.lua b/nvim/.config/nvim/init.lua index 07ac756..be86352 100644 --- a/nvim/.config/nvim/init.lua +++ b/nvim/.config/nvim/init.lua @@ -93,6 +93,11 @@ vim.g.clipboard = { cache_enabled = true } +-- No default mappings for tmux navigator +vim.g.tmux_navigator_no_mappings = 1 +-- Write the current buffer, but only if changed before navigating from Vim to tmux pane +vim.g.tmux_navigator_save_on_switch = 1 + -- We do this to prevent the loading of the system fzf.vim plugin. This is -- present at least on Arch/Manjaro vim.api.nvim_command('set rtp-=/usr/share/vim/vimfiles') diff --git a/nvim/.config/nvim/lua/keymappings.lua b/nvim/.config/nvim/lua/keymappings.lua index 86c04ea..e36b690 100644 --- a/nvim/.config/nvim/lua/keymappings.lua +++ b/nvim/.config/nvim/lua/keymappings.lua @@ -1,6 +1,7 @@ -local remap = vim.api.nvim_set_keymap -local expr_opts = { noremap=true, unique=true, expr=true } -local opts = { noremap=true, unique=true } +local remap = vim.api.nvim_set_keymap +local expr_opts = { noremap=true, unique=true, expr=true } +local silent_opts = { noremap=true, unique=true, silent=true } +local opts = { noremap=true, unique=true } remap('c', '', '', opts) remap('n', '', ':noh', opts) @@ -144,3 +145,9 @@ remap('n', ',D', 'lua vim.diagnostic.open_float(0, {scope="cursor"})', remap('n', 'dc', ':DiffConflicts' , opts) remap('n', 'ds', ':DiffConflictsShowHistory', opts) remap('n', 'dw', ':DiffConflictsWithHistory', opts) + +-- Tmux pane and nvim window switching +remap('n', '' , ':TmuxNavigateLeft' , silent_opts) +remap('n', '', ':TmuxNavigateRight', silent_opts) +remap('n', '' , ':TmuxNavigateUp' , silent_opts) +remap('n', '' , ':TmuxNavigateDown' , silent_opts) diff --git a/nvim/.config/nvim/lua/plugins.lua b/nvim/.config/nvim/lua/plugins.lua index c478932..42ed5cc 100644 --- a/nvim/.config/nvim/lua/plugins.lua +++ b/nvim/.config/nvim/lua/plugins.lua @@ -101,6 +101,7 @@ local init = function () 'chentau/marks.nvim', } use 'andymass/vim-matchup' + use 'christoomey/vim-tmux-navigator' end return require('packer').startup(init) diff --git a/tmux/.tmux.conf b/tmux/.tmux.conf index 26a0ab3..398541f 100644 --- a/tmux/.tmux.conf +++ b/tmux/.tmux.conf @@ -35,11 +35,26 @@ set-option -g allow-rename off set -g mouse on set -g focus-events on -# Use Ctrl-arrow keys without prefix key to switch panes -bind -n C-Left select-pane -L -bind -n C-Right select-pane -R -bind -n C-Up select-pane -U -bind -n C-Down select-pane -D +# 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|n?vim?x?)(diff)?$'" + +bind-key -n 'C-Left' if-shell "$is_vim" 'send-keys C-Left' 'select-pane -L' +bind-key -n 'C-Down' if-shell "$is_vim" 'send-keys C-Down' 'select-pane -D' +bind-key -n 'C-Up' if-shell "$is_vim" 'send-keys C-Up' 'select-pane -U' +bind-key -n 'C-Right' if-shell "$is_vim" 'send-keys C-Right' '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-Left' select-pane -L +bind-key -T copy-mode-vi 'C-Down' select-pane -D +bind-key -T copy-mode-vi 'C-Up' select-pane -U +bind-key -T copy-mode-vi 'C-Right' select-pane -R # Pane resizing with arrow keys bind Left resize-pane -L 10 diff --git a/tmux/.tmux/plugins/vim-tmux-navigator b/tmux/.tmux/plugins/vim-tmux-navigator new file mode 160000 index 0000000..9ca5bfe --- /dev/null +++ b/tmux/.tmux/plugins/vim-tmux-navigator @@ -0,0 +1 @@ +Subproject commit 9ca5bfe5bd274051b5dd796cc150348afc993b80