Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle neovim terminal buffer like nerdtree plugin [closed]

since we have the option to have a terminal inside a neovim buffer. I would very much like to have a way to "toggle" the buffer containing the terminal and have it appear in a fixed position like say the bottom of the screen.

I know that nerdtree does this for me, it toggles with a keybinding to always appear on my left side of the screen. what i wish for is the same with the terminal buffer in neovim. Is there anyone who knows of a plugin like this or how i would create one?

like image 846
Kristoffer Avatar asked Dec 11 '22 16:12

Kristoffer


2 Answers

That's my solution to anyone who wanna hide/show a single neovim terminal window of any height.

The terminal will show at very bottom in insert mode. If you wanna change the split behaviour just edit botright new to something else. :help opening-window

let g:term_buf = 0
let g:term_win = 0

function! Term_toggle(height)
    if win_gotoid(g:term_win)
        hide
    else
        botright new
        exec "resize " . a:height
        try
            exec "buffer " . g:term_buf
        catch
            call termopen($SHELL, {"detach": 0})
            let g:term_buf = bufnr("")
        endtry
        startinsert!
        let g:term_win = win_getid()
    endif
endfunction


nnoremap <M-t> :call Term_toggle(10)<cr>
tnoremap <M-t> <C-\><C-n>:call Term_toggle(10)<cr>
like image 63
user3762200 Avatar answered Dec 28 '22 10:12

user3762200


I might have a solution for you. The code below toggles a terminal on the far left with the f4-button:

let g:term_buf = 0
function! Term_toggle()
  1wincmd w
  if g:term_buf == bufnr("")
    setlocal bufhidden=hide
    close
  else
    topleft vnew
    try
      exec "buffer ".g:term_buf
    catch
      call termopen("bash", {"detach": 0})
      let g:term_buf = bufnr("")
    endtry
    startinsert!
  endif
endfunction
nnoremap <f4> :call Term_toggle()<cr>
like image 37
jonathf Avatar answered Dec 28 '22 09:12

jonathf