/
tmux_navigator.vim
65 lines (56 loc) · 2.02 KB
/
tmux_navigator.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
" Maps <C-h/j/k/l> to switch vim splits in the given direction. If there are
" no more windows in that direction, forwards the operation to tmux.
" Additionally, <C-\> toggles between last active vim splits/tmux panes.
if exists("g:loaded_tmux_navigator") || &cp || v:version < 700
finish
endif
let g:loaded_tmux_navigator = 1
function! s:UseTmuxNavigatorMappings()
return !exists("g:tmux_navigator_no_mappings") || !g:tmux_navigator_no_mappings
endfunction
function! s:InTmuxSession()
return $TMUX != ''
endfunction
let s:tmux_is_last_pane = 0
au WinEnter * let s:tmux_is_last_pane = 0
" Like `wincmd` but also change tmux panes instead of vim windows when needed.
function! s:TmuxWinCmd(direction)
if s:InTmuxSession()
call s:TmuxAwareNavigate(a:direction)
else
call s:VimNavigate(a:direction)
endif
endfunction
function! s:TmuxAwareNavigate(direction)
let nr = winnr()
let tmux_last_pane = (a:direction == 'p' && s:tmux_is_last_pane)
if !tmux_last_pane
" try to switch windows within vim
exec 'wincmd ' . a:direction
endif
" Forward the switch panes command to tmux if:
" a) we're toggling between the last tmux pane;
" b) we tried switching windows in vim but it didn't have effect.
if tmux_last_pane || nr == winnr()
let cmd = 'tmux select-pane -' . tr(a:direction, 'phjkl', 'lLDUR')
silent call system(cmd)
let s:tmux_is_last_pane = 1
else
let s:tmux_is_last_pane = 0
endif
endfunction
function! s:VimNavigate(direction)
execute 'wincmd ' . a:direction
endfunction
command! TmuxNavigateLeft call <SID>TmuxWinCmd('h')
command! TmuxNavigateDown call <SID>TmuxWinCmd('j')
command! TmuxNavigateUp call <SID>TmuxWinCmd('k')
command! TmuxNavigateRight call <SID>TmuxWinCmd('l')
command! TmuxNavigatePrevious call <SID>TmuxWinCmd('p')
if s:UseTmuxNavigatorMappings()
nmap <silent> <c-h> :TmuxNavigateLeft<cr>
nmap <silent> <c-j> :TmuxNavigateDown<cr>
nmap <silent> <c-k> :TmuxNavigateUp<cr>
nmap <silent> <c-l> :TmuxNavigateRight<cr>
nmap <silent> <c-\> :TmuxNavigatePrevious<cr>
endif