From 8193f1e40eadc6822ed2af09583ef9f7e1055dcc Mon Sep 17 00:00:00 2001 From: Ramon Fischer Date: Thu, 6 Oct 2022 21:31:32 +0200 Subject: [PATCH 1/4] use internal macros and commands as much as possible to cause less load and invoking less subshells --- README.md | 26 +++++++++++++++----------- vim-tmux-navigator.tmux | 29 ++++++++++++++--------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 841023d..15f27f4 100644 --- a/README.md +++ b/README.md @@ -65,17 +65,21 @@ Add the following to your `~/.tmux.conf` file: ``` tmux # 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-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'" + +# pseudo substring match by substituting the current command from the current commmand string +bind-key -n C-k if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-k" "select-pane -U" +bind-key -n C-j if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-j" "select-pane -D" +bind-key -n C-h if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-h" "select-pane -L" +bind-key -n C-l if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-l" "select-pane -R" +# bring back clear screen (PREFIX + CTRL + l) +bind-key C-l send-keys "C-l" + +# if "${tmux_version}" is greater or equal "3.0", then "send-keys C-\\\\", else "send-keys C-\\" +# https://unix.stackexchange.com/a/285928 +tmux_version="#{version}" +if-shell -b "[ #(printf '%s\n' '3.0' '${tmux_version}' | sort --version-sort | head --lines='1') ]" \ + "bind-key -n C-\\ 'send-keys C-\\\\' 'select-pane -l'" \ + "bind-key -n C-\\ '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 diff --git a/vim-tmux-navigator.tmux b/vim-tmux-navigator.tmux index 0aeac55..838a006 100755 --- a/vim-tmux-navigator.tmux +++ b/vim-tmux-navigator.tmux @@ -1,22 +1,21 @@ #!/usr/bin/env bash -version_pat='s/^tmux[^0-9]*([.0-9]+).*/\1/p' +# pseudo substring match by substituting the current command from the current commmand string +tmux bind-key -n C-k if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-k" "select-pane -U" +tmux bind-key -n C-j if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-j" "select-pane -D" +tmux bind-key -n C-h if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-h" "select-pane -L" +tmux bind-key -n C-l if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-l" "select-pane -R" +# bring back clear screen (PREFIX + CTRL + l) +tmux bind-key C-l send-keys "C-l" -is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ - | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'" -tmux bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L" -tmux bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D" -tmux bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U" -tmux bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R" -tmux_version="$(tmux -V | sed -En "$version_pat")" -tmux setenv -g tmux_version "$tmux_version" +# if "${tmux_version}" is greater or equal "3.0", then "send-keys C-\\\\", else "send-keys C-\\" +# https://unix.stackexchange.com/a/285928 +tmux_version=$(tmux display-message -p "#{version}") +tmux setenv -g tmux_version "${tmux_version}" -#echo "{'version' : '${tmux_version}', 'sed_pat' : '${version_pat}' }" > ~/.tmux_version.json - -tmux if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \ - "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\' 'select-pane -l'" -tmux if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \ - "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\' 'select-pane -l'" +tmux if-shell -b "[ #(printf '%s\n' '3.0' '${tmux_version}' | sort --version-sort | head --lines='1') ]" \ + "bind-key -n C-\\ 'send-keys C-\\\\' 'select-pane -l'" \ + "bind-key -n C-\\ 'send-keys C-\\' 'select-pane -l'" tmux bind-key -T copy-mode-vi C-h select-pane -L tmux bind-key -T copy-mode-vi C-j select-pane -D From 3153fc7ccb80342de1a9d55efd0ae29d01d32543 Mon Sep 17 00:00:00 2001 From: Ramon Fischer Date: Thu, 6 Oct 2022 21:36:22 +0200 Subject: [PATCH 2/4] add note about removing everything but the below snippet --- plugin/tmux_navigator.vim | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugin/tmux_navigator.vim b/plugin/tmux_navigator.vim index a64e5dc..8ed2c26 100644 --- a/plugin/tmux_navigator.vim +++ b/plugin/tmux_navigator.vim @@ -2,6 +2,22 @@ " no more windows in that direction, forwards the operation to tmux. " Additionally, toggles between last active vim splits/tmux panes. +" i am not sure about this one, but the below commented snipped is sufficient. +" vim-tmux-navigator +" function! TmuxMove(direction) +" let wnr = winnr() +" silent! execute 'wincmd ' . a:direction +" " if the winnr is still the same after we moved, it is the last pane. +" if wnr == winnr() +" call system('tmux select-pane -' . tr(a:direction, 'phjkl', 'lLDUR')) +" end +" endfunction +" +" nnoremap :call TmuxMove('h') +" nnoremap :call TmuxMove('j') +" nnoremap :call TmuxMove('k') +" nnoremap :call TmuxMove('l') + if exists("g:loaded_tmux_navigator") || &cp || v:version < 700 finish endif From 43f22531df137a5fc248f9378fa2540904498aa9 Mon Sep 17 00:00:00 2001 From: Ramon Fischer Date: Thu, 6 Oct 2022 21:54:44 +0200 Subject: [PATCH 3/4] position comment correctly --- vim-tmux-navigator.tmux | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vim-tmux-navigator.tmux b/vim-tmux-navigator.tmux index 838a006..4142f65 100755 --- a/vim-tmux-navigator.tmux +++ b/vim-tmux-navigator.tmux @@ -8,11 +8,11 @@ tmux bind-key -n C-l if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim # bring back clear screen (PREFIX + CTRL + l) tmux bind-key C-l send-keys "C-l" -# if "${tmux_version}" is greater or equal "3.0", then "send-keys C-\\\\", else "send-keys C-\\" -# https://unix.stackexchange.com/a/285928 tmux_version=$(tmux display-message -p "#{version}") tmux setenv -g tmux_version "${tmux_version}" +# if "${tmux_version}" is greater or equal "3.0", then "send-keys C-\\\\", else "send-keys C-\\" +# https://unix.stackexchange.com/a/285928 tmux if-shell -b "[ #(printf '%s\n' '3.0' '${tmux_version}' | sort --version-sort | head --lines='1') ]" \ "bind-key -n C-\\ 'send-keys C-\\\\' 'select-pane -l'" \ "bind-key -n C-\\ 'send-keys C-\\' 'select-pane -l'" From dd211c8bd39f22c25ead5ad898e38698ef2accb1 Mon Sep 17 00:00:00 2001 From: Ramon Fischer Date: Thu, 6 Oct 2022 22:13:45 +0200 Subject: [PATCH 4/4] correct comment --- README.md | 2 +- vim-tmux-navigator.tmux | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 15f27f4..1ceee61 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Add the following to your `~/.tmux.conf` file: # Smart pane switching with awareness of Vim splits. # See: https://github.com/christoomey/vim-tmux-navigator -# pseudo substring match by substituting the current command from the current commmand string +# pseudo substring match by substituting "g?(view|n?vim?x?)(diff)?$" from the current commmand string bind-key -n C-k if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-k" "select-pane -U" bind-key -n C-j if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-j" "select-pane -D" bind-key -n C-h if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-h" "select-pane -L" diff --git a/vim-tmux-navigator.tmux b/vim-tmux-navigator.tmux index 4142f65..0df9ada 100755 --- a/vim-tmux-navigator.tmux +++ b/vim-tmux-navigator.tmux @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# pseudo substring match by substituting the current command from the current commmand string +# pseudo substring match by substituting "g?(view|n?vim?x?)(diff)?$" from the current commmand string tmux bind-key -n C-k if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-k" "select-pane -U" tmux bind-key -n C-j if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-j" "select-pane -D" tmux bind-key -n C-h if-shell "[ '#{pane_current_command}' != '#{s/g?(view|n?vim?x?)(diff)?$//:#{pane_current_command}}' ]" "send-keys C-h" "select-pane -L"