diff --git a/README.md b/README.md index 841023d..1ceee61 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 "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 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 diff --git a/vim-tmux-navigator.tmux b/vim-tmux-navigator.tmux index 0aeac55..0df9ada 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 "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