Skip to content

Instantly share code, notes, and snippets.

@issadarkthing
Last active December 17, 2020 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save issadarkthing/a8b04b16ff12c15f7bf57c9c89b36605 to your computer and use it in GitHub Desktop.
Save issadarkthing/a8b04b16ff12c15f7bf57c9c89b36605 to your computer and use it in GitHub Desktop.
toggle neovim terminal window
let s:term_window = -1
let s:term_buffer = -1
let s:term_job_id = -1
function! TerminalOpen()
" Check if buffer exists, if not create a window and a buffer
if !bufexists(s:term_buffer)
" Creates a window call monkey_terminal
new local_terminal
" disable line numbers
setlocal nonumber norelativenumber
" Moves to the window the right the current one
" wincmd L
let s:term_job_id = termopen($SHELL, { 'detach': 1 })
" Change the name of the buffer to "Terminal 1"
silent file Terminal\ 1
" Gets the id of the terminal window
let s:term_window = win_getid()
let s:term_buffer = bufnr('%')
" The buffer of the terminal won't appear in the list of the buffers
" when calling :buffers command
set nobuflisted
else
if !win_gotoid(s:term_window)
split
" Moves to the window below the current one
" wincmd L
buffer Terminal\ 1
" Gets the id of the terminal window
let s:term_window = win_getid()
endif
endif
" be in insert mode immediately
startinsert
endfunction
function! TerminalToggle()
if win_gotoid(s:term_window)
hide
else
call TerminalOpen()
endif
endfunction
" toggle terminal window
nnoremap <silent> <C-z> :call TerminalToggle()<cr>
tnoremap <silent> <C-z> <C-\><C-n>:call TerminalToggle()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment