Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise vi detection logic and use internal macros and commands as much as possible #321

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "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"
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
Expand Down
16 changes: 16 additions & 0 deletions plugin/tmux_navigator.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
" no more windows in that direction, forwards the operation to tmux.
" Additionally, <C-\> 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 <silent> <c-h> :call TmuxMove('h')<cr>
" nnoremap <silent> <c-j> :call TmuxMove('j')<cr>
" nnoremap <silent> <c-k> :call TmuxMove('k')<cr>
" nnoremap <silent> <c-l> :call TmuxMove('l')<cr>

if exists("g:loaded_tmux_navigator") || &cp || v:version < 700
finish
endif
Expand Down
29 changes: 14 additions & 15 deletions vim-tmux-navigator.tmux
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#!/usr/bin/env bash

version_pat='s/^tmux[^0-9]*([.0-9]+).*/\1/p'
# 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"
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"
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'"
# 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'"

tmux bind-key -T copy-mode-vi C-h select-pane -L
tmux bind-key -T copy-mode-vi C-j select-pane -D
Expand Down