Skip to content

Instantly share code, notes, and snippets.

@tarruda
Created July 9, 2014 10:49
Show Gist options
  • Save tarruda/1b5010531bb6ae7382a4 to your computer and use it in GitHub Desktop.
Save tarruda/1b5010531bb6ae7382a4 to your computer and use it in GitHub Desktop.
Neovim job control demo
" Demo of Neovim job control feature.
"
" It starts two netcat processes listening on the same TCP port,
" the second process will exit immediately since the port will be
" unavailable(Used to demonstrate the stderr/exit events)
" To play with this, use two terminals
"
" On terminal 1: `nvim -S jobcontrol.vim`.
" Use `:call jobwrite(v:srv1_id, string) to write
" data to the netcat client
" On terminal 2: `nc localhost 9991`. Type lines to send data to
" nvim
"
" Use `jobstop(job_id)` to stop a running job
set nocp
" Arguments to jobstart:
" - The job "name", this will be used mainly to filter JobActivity autocommands
" - The program name. Can be the full path to the program or a $PATH entry
" - An array with the program arguments
:let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '9991'])
:let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '9991'])
function JobEvent()
" v:job_data[0] = the job id
" v:job_data[1] = the event type, one of "stdout", "stderr" or "exit"
" v:job_data[2] = data read from stdout or stderr
if v:job_data[1] == 'stdout'
let str = 'Message from job '.v:job_data[0].': '.v:job_data[2]
elseif v:job_data[1] == 'stderr'
let str = 'Error message from job '.v:job_data[0].': '.v:job_data[2]
else
" Exit
let str = 'Job '.v:job_data[0].' exited'
endif
call append(line('$'), str)
endfunction
" This autocommand will run whenever there's activity on jobs with names
" starting with 'netcat-server-'
au JobActivity netcat-server-* call JobEvent()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment