4

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?

Kristoffer
  • 456
  • 5
  • 17

4 Answers4

7

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>
user3762200
  • 447
  • 1
  • 6
  • 17
3

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>
jonathf
  • 586
  • 5
  • 5
  • Thanks! That actually works somewhat! i am trying to get it to start work for the bottom, but it refuses to toggle, it jus opens a new buffer window instead. i changed the line: 'topleft vnew' to 'botright new' any ideas? – Kristoffer May 16 '16 at 11:22
  • The command also adds a lot of [No name] buffers – Kristoffer May 16 '16 at 11:30
  • I am not very familiar with remote windows, but I would think that you could use `windo` here. Send a command to all windows, and save `winnr()` for the window that are terminal buffers to a global variable. Then switch to that window and do the stuff from my function. – jonathf May 19 '16 at 07:07
  • As for the `[no name]`, I'm not sure what is going on. If the `exec` term fails every time it is run, then obviously it will respawn and hide a lot of buffers, but I thought the code dealt with that. Is your terminal the same one on each toggle, or do you get a new spawn? – jonathf May 19 '16 at 07:13
  • No i checked that, the exec succeds everytime. Also i changed the 'call termopen...' to 'terminal' gives the same results. The terminal is the same even thought it creates a lot of [no name]-buffers. I tried to modify it so instead of 1wincmd i would use '"sbuffer"g:term_buf", but that didnt work – Kristoffer May 19 '16 at 14:24
  • I'm not getting that behavior on my side, and I'm not sure what it could be. Hope you find a solution. – jonathf May 22 '16 at 10:21
2

I Think this must be a little better more IDE Like,

let g:term_buf = 0
function! Term_toggle()
  1wincmd w
  if g:term_buf == bufnr("")
    setlocal bufhidden=hide
    close
  else
    rightbelow new
    12winc -
    try
      exec "buffer ".g:term_buf
    catch
      call termopen("bash", {"detach": 0})
      let g:term_buf = bufnr("")
    endtry
    set laststatus=0
    startinsert!
  endif
endfunction
nnoremap <f4> :call Term_toggle()<cr>

" Terminal go back to normal mode
tnoremap <Esc> <C-\><C-n>
" When switching to terminal windows it goes into insert mode automatically
au BufEnter * if &buftype == 'terminal' | :startinsert | endif
1

An simple solution, but it deletes the buffer:

nnoremap <silent> <F3> :split term://zsh <CR>
tnoreamp <silent> <F3> <C-\><C-n> :bd! <CR>
autocmd TermOpen * startinsert

the terminal buffer needs to be selected to be closed!