I'm new to vimscript and writing a function to toggle a terminal buffer. Here is what I tried:
let g:toggle_term = "<C-j>" let g:term_buf_nr = -1 function! ToggleTerminal() if g:term_buf_nr == -1 execute "bot term" let g:term_buf_nr = bufnr("$") else execute "bd! " .g:term_buf_nr let g:term_buf_nr = -1 endif endfunction execute "nnoremap ".g:toggle_term ." :call ToggleTerminal()<CR>"
The function works fine and toggles the terminal buffer only in case the terminal buffer itself is not focused. If it is focused then pressing the
g:toggle_term combination does nothing.
Is there a way make
g:toggle_term to toggle terminal even if the terminal buffer is currently focused?