Andrey Listopadov

Managing background processes from Kakoune

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 a server for my application in the background, and so on. And actually, I use this a lot while working on this blog - I run hugo process in the background to see how my page is looking. This got me thinking about handling all of this in Kakoune.

Kakoune has facilities to run processes in the background, and it does this for many tools like grep, make, ctags, and other common shell tools that we use often. Kakoune also has built-in REPL support with repl and send-text commands, which 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 the background. This is due to the architecture of the project, and I can’t really do anything about it. So when I’m programming in Emacs, an 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 is that there’s no built-in way that I can think of. There are ! and | commands to run a synchronous process and pipe its result into a buffer, which is not for me. I’d also like to kill a process if I don’t need it, and see its log as if it ran in a terminal. Turns out it isn’t a really hard task.

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

  • pstart <command>: Starts command as a background process, and creates 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 a 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 am 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.