Skip to content

Instantly share code, notes, and snippets.

@davidmh
Last active June 22, 2016 16:48
Show Gist options
  • Save davidmh/728f873302ada1ba16fd0520dcd7530a to your computer and use it in GitHub Desktop.
Save davidmh/728f873302ada1ba16fd0520dcd7530a to your computer and use it in GitHub Desktop.
Mappings and basic REPL for Neovim's :term
" Term bindings
"
" Spawns
" open shell on the current window
nnoremap <c-t><c-e> :e term://$SHELL<CR>A
" open vertical shell
nnoremap <c-t><c-v> <c-w><c-v><c-w><c-l>:e term://$SHELL<CR>A
" open horizontal shell
nnoremap <c-t><c-s> <c-w><c-s><c-w><c-j>:e term://$SHELL<CR>A
"
" terminal options
au! TermOpen * call OnTermOpen()
au! TermClose * call OnTermClose()
function! OnTermOpen()
" show line numbers
setlocal relativenumber number
" highlight current line
setlocal cursorline
" tmux navigator fix
nnoremap <buffer> <silent> <BS> :TmuxNavigateLeft<CR>
" terminal job id for the REPL
let g:terminal_id = b:terminal_job_id
endfunction
function! OnTermClose()
" clear the terminal job reference
let g:terminal_id = 0
endfunction
let g:terminal_id = 0
"
" enter normal mode while on a terminal
" and go to the last prompt
tnoremap <silent> <esc> <c-\><c-n>:call MoveToLastPrompt()<CR>
function! MoveToLastPrompt()
" move to the end ef the line and
" search for any non-empty caracter
$?.
" move to the end of the prompt
normal! $l
endfunction
"
" REPL
function! SendLinesToREPL(lines)
if g:terminal_id
call jobsend(g:terminal_id, add(a:lines, ''))
else
echo "Can't find any open terminals"
endif
endfunction
nnoremap <expr> <silent> <leader>sl SendLinesToREPL([getline('.')])
vnoremap <expr> <silent> <leader>sl SendLinesToREPL(getline(line('v'), line('.')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment