I have a vimscript function which uses a combination of vimscript functions and also some calls to :call system()
. The vimscript and the system()
calls can be 5-10 seconds slow, during which time, Vim is frozen. How do I defer the entire script to another thread? I read online that timer_start()
should work, but it doesn't for me.
I tried this:
function! SlowCall()
let text = system('sleep 3 ; ls') " An example of a slow system call + some return value
echo text
" ... imagine more vimscript functions that use `text`, below ...
endfunction
call timer_start(0, { -> SlowCall() })
If I source the above script, Vim prevents typing for 3 seconds, instead of running in the background while I continue to work. How do I run SlowCall()
in the background without interrupting typing?
sleep
system()
itself? I'll try it when I'm able.system()
or any other arbitrary VimScript function and expect it to run asynchronously. There's a dedicated API for running external processes. See:h job
. Note that API is incompatible across Vim / Neovim. There are some "compat plugins" but you only need'em if you target both editors and/or find Job API too difficult to use as is.timer_start
is not really async stuff, instead, it allows to schedule execution of some Vimscript to a later time (or to run several times/in loops). But theSlowCall
function will occupy the "Vimscript thread". The only solution, as far as I know, is to rely on external threads in other languages (e.g. Python). For this, thejob
backend is useful, see:help job-start
.timer_start()
) but I think you're right. Single-threaded through and through. I've got a couple answers to edit. :P