Forked from ram535/gist:b1b7af6cd7769ec0481eb2eed549ea23
Last active
December 2, 2019 03:33
-
-
Save yen3/8c0f7dd012cd8b3b9b07b6992c82e96f to your computer and use it in GitHub Desktop.
Reuse the same terminal in neovim. (Open the terminal in below)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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