TIL: Background jobs with vim
I use Zola
to author this blog and it features a very helpful zola serve
command
that rebuilds and refreshes the page on the browser in response to each change.
However, opening a separate terminal tab to run this command is not very convenient. Especially when most of the time I don't care about the outputs: if it builds successfully, then the result I care about is in the browser, not in the terminal.
Today I learned that vim has a way of running jobs in the background, and it is very handy for this kind of command.
If you run
:let job = job_start('zola serve --open')
the job will run in the background and be killed automatically when vim is closed.
That last part is important, because if you had run:
:!zola serve --open &>/dev/null &
the job would remain running in the background after vim is closed. You'd have to remember to kill it manually.
In any case, if you want to kill the job without closing vim, you can simply:
:call job_stop(job) " job is the variable name used in the first command
And if you want to run the command, but see its outputs, there is also term_start
:
:call term_start('zola serve --open')
which will show the command's output in a buffer.