Managing background processes from Kakoune
kakoune tools ~2 minutes read

While working on my previous post I was mainly using Emacs, because it has the best support for Lisp languages. It has great integration with the REPL, can run server for my application in background and so on. And actually, I use this a lot while working on this blog - I run hugo process in background to see how my page is looking. This got me thinking about handling all of this in Kakoune.

Kakoune has facilities to run process in background, and it does this for many tools like grep, make, ctags, and other common shell tools that we use often. Kakoune also has builtin REPL support with repl and send-text commands, that can be used when I work with Lisps. However there’s a problem.

When working with Clojure, I need to run two JVM instances in background. This is due to architecture of the project, and I can’t really do anything to it. So when I’m programming in Emacs, amazing package, named CIDER(Clojure IDE that Rocks) does this for me. But in order to do this in Kakoune I have to start another terminal, or create a new window in Tmux, and I don’t really like this.

So I’ve thought, why not start this from Kakoune itself? The problem though, that there’s no builtin way that I can think of. There are ! and | commands to run synchronous process and pipe it’s result into buffer, which is not for me. I’d also would like to kill process if I don’t need it, and see it’s log as if it ran in terminal. Turns out it isn’t really hard task.

I’ve created pmanage.kak plugin to test this idea. This plugin quite small and has these three commands:

  • pstart <command>: Starts command as background process, and create FIFO buffer for that process named after process PID.
  • pstop <PID>: delete process buffer and send SIGTERM signal to process.
  • pkill <PID>: delete process buffer and send SIGKILL signal to process.

So now, I can do this:

  • open Kakoune in project root;
  • call :pstart 'npx shadow-cljs watch app' to start background watching process.
    • A new FIFO buffer is created, and process PID is stored into special option;
  • Then I can do :repl 'npx shadow-cljs cljs-repl app' and open REPL window that will be connected to that watcher process;
  • Develop!

I also now able to run :pstart 'hugo server -D', and edit any Markdown files that my blog is made of. And when I don’t need that process I simply can call :pstop PID or :delete-buffer PID.

I know about kakoune:1414, but I think the solution I came up with is fine for most use cases.