Start subprocess from kakoune

Hello,

In bash I can easily start a new subprocess with something like:

start_cmd &

or even

(start_cmd &)

But from kakoune I didn’t find a way to do the same inside a %sh{ } block

From all what I have tried, either I get a “waiting for shell script to finish” or the process doesn’t start.

What am I doing wrong?

A %sh{} block captures the stderr and stdout of the shell process, so it waits for the shell process to close its pipes before it continues. In bash, you can run:

(sleep 5; echo hello) &

…and five seconds later, “hello” will blurt out in the middle of whatever you’re doing - even though the subshell is in the background, it’s still connected to the existing stdout. The way around this is to redirect the output pipes as well as run the process in the background:

( { sleep 5; echo hello; } & ) >/dev/null 2>/dev/null

Now the child process is isolated from the parent process, and Kakoune won’t hang waiting for it to finish.

Note: You might wonder why the above example applies & in one set of brackets, then applies the redirections with another, instead of doing them all at the same time. This works around a bug in OpenBSD’s shell.

3 Likes

Wow ok I could I never found it myself. Thanks!

I’m thinking it could be nice to have an already buildin command commin with kakoune, since this is not very intuitive. Something like:

spawn_subprocess () {
 ( { $* } & ) >/dev/null 2>/dev/null
}