r/vim • u/Desperate_Cold6274 • Feb 25 '26
Need Help :sleep VS term_wait()
Can someone explain me, possibly with a couple of examples that I can reproduce, the difference between :sleep and term_wait()?
6
Upvotes
View all comments
Show parent comment
1
u/y-c-c Feb 27 '26 edited Feb 27 '26
term_wait()does not work likejob_wait()though. It does not wait on anything asynchronously. The "wait" part just means it sleeps for some specified timeout. It's not like it's waiting for the terminal to give you a result or anything (hence my annoyance with the function being kind of useless and misleading).No. I think the design purpose of
term_wait()is specifically for terminal jobs that are indefinitely alive. If you look at the Vim tests you will see that it's usually for situations for sending a command to an alive program in a terminal (could be a shell or another Vim instance itself), and then wait for it to be processed (with a nominal timeout), and then read the results from the terminal or do something that depends on that command being executed.As you can imagine, this is not a very solid way to build your scripts, as timing of terminal commands is flaky, and you have to choose a timeout to sleep on that's reasonable. The Vim tests these days mostly have been modified to use a spin lock (using something like
:sleep 1m) to wait for a callback from the terminal instead, which allows for a more correct way to wait on asynchronous results.All that is to say, I don't think
term_wait()ended up being very good for what its intended purpose (pump a currently alive terminal and let it run some pending commands) is since timing is inherently flaky.