Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danijar
Last active June 29, 2022 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danijar/ba4de0b2002b52ef9263dc01f57efd82 to your computer and use it in GitHub Desktop.
Save danijar/ba4de0b2002b52ef9263dc01f57efd82 to your computer and use it in GitHub Desktop.
Seamless focus switching between Vim splits, Vim tabs, and Tmux panes.
# Forward movements commands if Vim is focused.
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind -n C-h if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind -n C-j if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind -n C-k if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind -n C-l if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Focus Movements
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! SplitTabPaneRight()
let win = winnr()
wincmd l
if win != winnr()
return
elseif tabpagenr() != tabpagenr('$')
normal gt
else
:call system('tmux select-pane -R')
endif
endfunction
function! SplitTabPaneLeft()
let win = winnr()
wincmd h
if win != winnr()
return
elseif tabpagenr() != 1
normal gT
else
:call system('tmux select-pane -L')
endif
endfunction
function! SplitTabDown()
let win = winnr()
wincmd j
if win == winnr()
:call system('tmux select-pane -D')
endif
endfunction
function! SplitPaneUp()
let win = winnr()
wincmd k
if win == winnr()
:call system('tmux select-pane -U')
endif
endfunction
nnoremap <silent> <c-h> :call SplitTabPaneLeft()<cr>
nnoremap <silent> <c-l> :call SplitTabPaneRight()<cr>
nnoremap <silent> <c-j> :call SplitTabDown()<cr>
nnoremap <silent> <c-k> :call SplitPaneUp()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment