" 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 L 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 else if !win_gotoid(s:monkey_terminal_window) sp " Moves to the window below the current one wincmd L 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 :call MonkeyTerminalToggle() tnoremap :call MonkeyTerminalToggle() " 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 :call MonkeyTerminalExec('go run ' . expand('%')) augroup END