I was trying to create a trigger that does the following: When opening a new tab with a file, if there already exists a terminal buffer, it is opened below in this new tab.
I have seen in logfiles that the events triggered when opening a new tab are WinLeave
, TabLeave
, WinEnter
and TabEnter
(in this order). In my .vimrc
file I created an augroup and tried to use autocmds.
The problem is that it does not work as expected (it does nothing, actually), and I am pretty sure that the wrong part is related to sequential autocmd.
Here is the code:
autocmd!
" Take into account events in their order
autocmd WinLeave ++nest autocmd TabLeave
\ ++nest autocmd WinEnter ++nest autocmd TabEnter * |
" Create variable with existing terminal buffers
\ let term_buf = filter(range(1, bufnr('$')),
\ 'getbufvar(v:val, "&buftype", "ERROR") == "terminal"') |
\ if len(term_buf) > 0 |
" First terminal buffer taken
\ let x = term_buf[0] |
" Open new buffer below
\ exe 'below sbuffer'.x |
\ startinsert |
\ endif
augroup end