Skip to content

Commit

Permalink
do window edge detection in Vim; no more timeouts!
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaku committed Jul 9, 2020
1 parent 053a246 commit 8e5823a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
7 changes: 0 additions & 7 deletions README.md
Expand Up @@ -40,13 +40,6 @@ set -g @navigate-down '-n M-n'
set -g @navigate-right '-n M-s'
```

4. Timeout for very slow Vim (optional):
```sh
# set this ONLY IF your Vim is very slow
set -g @navigate-timeout 1.618 # seconds
# propagation delay for Vim title change
```

5. Reload your tmux configuration file.

6. Type <kbd>prefix</kbd>+<kbd>I</kbd>.
Expand Down
19 changes: 18 additions & 1 deletion plugin/tmux-navigate.vim
Expand Up @@ -2,5 +2,22 @@
" This also supports SSH tunnels where Vim is running on a remote host.
" See https://sunaku.github.io/tmux-select-pane.html for documentation.
let progname = substitute($VIM, '.*[/\\]', '', '')
set title titlestring=%{progname}\ %f\ +%l\ #%{tabpagenr()}.%{winnr()}
set title titlestring=%{progname}\ %f\ +%l\ #%{TmuxNavigateDirections()}
if &term =~ '^screen' && !has('nvim') | exe "set t_ts=\e]2; t_fs=\7" | endif

function! TmuxNavigateDirections() abort
let [y, x] = win_screenpos('.')
let h = winheight('.')
let w = winwidth('.')

let can_go_up = y > 2 " +1 for the tabline
let can_go_down = y + h < &lines - &laststatus
let can_go_left = x > 1
let can_go_right = x + w < &columns

return
\ (can_go_up ? 'U' : '') .
\ (can_go_down ? 'D' : '') .
\ (can_go_left ? 'L' : '') .
\ (can_go_right ? 'R' : '')
endfunction
23 changes: 11 additions & 12 deletions tmux-navigate.tmux
Expand Up @@ -16,10 +16,7 @@

get_tmux_option() { tmux show-option -gqv "$@" | grep . ;}

timeout=$(get_tmux_option '@navigate-timeout') || timeout=0.05 # seconds
navigate=" \
vim_navigation_timeout=$timeout; \
"' \
navigate=' \
pane_title="#{q:pane_title}"; \
pane_current_command="#{q:pane_current_command}"; \
pane_is_zoomed() { \
Expand Down Expand Up @@ -50,18 +47,20 @@ navigate=" \
tmux_navigation_command=$1; \
vim_navigation_command=$2; \
vim_navigation_only_if=${3:-true}; \
navigate_tmux_if_unzoomed=true; \
if pane_contains_vim && eval "$vim_navigation_only_if"; then \
if pane_contains_neovim_terminal; then \
tmux send-keys C-\\ C-n; \
fi; \
eval "$vim_navigation_command"; \
if ! pane_is_zoomed; then \
sleep $vim_navigation_timeout; : wait for Vim to change title; \
if ! pane_title_changed; then \
eval "$tmux_navigation_command"; \
fi; \
fi; \
elif ! pane_is_zoomed; then \
vim_available_directions=l"${pane_title####* ##}"; \
tmux_navigate_direction=${tmux_navigation_command##* -}; \
tmux_navigate_direction=${tmux_navigate_direction%% *}; \
case "$vim_available_directions" in (*$tmux_navigate_direction*) \
navigate_tmux_if_unzoomed=false; \
eval "$vim_navigation_command";; \
esac; \
fi; \
if $navigate_tmux_if_unzoomed && ! pane_is_zoomed; then \
eval "$tmux_navigation_command"; \
fi; \
}; \
Expand Down

0 comments on commit 8e5823a

Please sign in to comment.