I was looking for a robust way to toggle a terminal and ended up building something that meets my expectations.
- Off: any visible terminal is hidden
- On: terminal as a bottom panel (restore first hidden terminal or create new one)
- Works with already open terminal
- Arguments: size and side of the terminal, defaults to 6 lines at the bottom
It seems pretty robust, but any idea to improve it is welcome.
function! PutTermPanel(buf, side, size) abort
" new term if no buffer
if a:buf == 0
term
else
execute "sp" bufname(a:buf)
endif
" default side if wrong argument
if stridx("hjklHJKL", a:side) == -1
execute "wincmd" "J"
else
execute "wincmd" a:side
endif
" horizontal split resize
if stridx("jkJK", a:side) >= 0
if ! a:size > 0
resize 6
else
execute "resize" a:size
endif
return
endif
" vertical split resize
if stridx("hlHL", a:side) >= 0
if ! a:size > 0
vertical resize 6
else
execute "vertical resize" a:size
endif
endif
endfunction
function! s:ToggleTerminal(side, size) abort
let tpbl=[]
let closed = 0
let tpbl = tabpagebuflist()
" hide visible terminals
for buf in filter(range(1, bufnr('$')), 'bufexists(bufname(v:val)) && index(tpbl, v:val)>=0')
if getbufvar(buf, '&buftype') ==? 'terminal'
silent execute bufwinnr(buf) . "hide"
let closed += 1
endif
endfor
if closed > 0
return
endif
" open first hidden terminal
for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)<0')
if getbufvar(buf, '&buftype') ==? 'terminal'
call PutTermPanel(buf, a:side, a:size)
return
endif
endfor
" open new terminal
call PutTermPanel(0, a:side, a:size)
endfunction
nnoremap <silent> yot :call <SID>ToggleTerminal('J', 6)<CR>
tnoremap
to have the mapping executed?:h :tnoremap
:h Terminal-Job
.nnoremap
only applies in Terminal-Normal mode.Terminal-mode
?"tnoremap ".g:toggle_term ." exit<CR> :call ToggleTerminal<CR>"
does not really call the function.