Skip to content

Instantly share code, notes, and snippets.

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 yen3/8c0f7dd012cd8b3b9b07b6992c82e96f to your computer and use it in GitHub Desktop.
Save yen3/8c0f7dd012cd8b3b9b07b6992c82e96f to your computer and use it in GitHub Desktop.
Reuse the same terminal in neovim. (Open the terminal in below)
" With this function you can reuse the same terminal in neovim.
" You can toggle the terminal and also send a command to the same terminal.
let s:monkey_terminal_window = -1
let s:monkey_terminal_buffer = -1
let s:monkey_terminal_job_id = -1
function! MonkeyTerminalOpen()
" Check if buffer exists, if not create a window and a buffer
if !bufexists(s:monkey_terminal_buffer)
" Creates a window call monkey_terminal
new monkey_terminal
" Moves to the window the right the current one
wincmd J
resize 10
let s:monkey_terminal_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:monkey_terminal_window = win_getid()
let s:monkey_terminal_buffer = bufnr('%')
" The buffer of the terminal won't appear in the list of the buffers
" when calling :buffers command
set nobuflisted
startinsert
else
if !win_gotoid(s:monkey_terminal_window)
sp
" Moves to the window below the current one
wincmd J
resize 10
buffer Terminal\ 1
" Gets the id of the terminal window
let s:monkey_terminal_window = win_getid()
endif
endif
endfunction
function! MonkeyTerminalToggle()
if win_gotoid(s:monkey_terminal_window)
call MonkeyTerminalClose()
else
call MonkeyTerminalOpen()
endif
endfunction
function! MonkeyTerminalClose()
if win_gotoid(s:monkey_terminal_window)
" close the current window
hide
endif
endfunction
function! MonkeyTerminalExec(cmd)
if !win_gotoid(s:monkey_terminal_window)
call MonkeyTerminalOpen()
endif
" clear current input
call jobsend(s:monkey_terminal_job_id, "clear\n")
" run cmd
call jobsend(s:monkey_terminal_job_id, a:cmd . "\n")
normal! G
wincmd p
endfunction
" With this maps you can now toggle the terminal
nnoremap <F7> :call MonkeyTerminalToggle()<cr>
tnoremap <F7> <C-\><C-n>:call MonkeyTerminalToggle()<cr>
" This an example on how specify command with different types of files.
augroup go
autocmd!
autocmd BufRead,BufNewFile *.go set filetype=go
autocmd FileType go nnoremap <F5> :call MonkeyTerminalExec('go run ' . expand('%'))<cr>
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment