A while ago, somebody popped into the Kakoune IRC channel inquiring about an equivalent to Vim’s :!
, which just runs a shell command in the foreground. Because of Kakoune’s client-server architecture, it can’t support that feature exactly, but I can see the appeal of running ls
or cal
without having to open another terminal.
I’ve also been thinking about Kakoune’s REPL features, :repl-new
that launches a command in a new terminal, and :repl-send-text
that sends the current selection to the active repl process. These tools can be very useful, but they’re somewhat limited - the tmux and kitty backends only work inside those respective tools, and the X11 backend depends on external tools (xdotool
, xsel
) and implicit assumptions (whether the terminal respects title-bar text, whether programs can change keyboard focus on their own, and whether <s-insert>
means “paste”) so it can be quite brittle.
I wondered how difficult it would be to teach Kakoune to run a REPL inside Kakoune, with commands to send data to the REPL process, and the output being plumbed into a FIFO buffer so you could scroll through it and copy/paste the results into other buffers, etc. I learned a lot about POSIX FIFO and pipe semantics, and had to invent some debugging tools, but it turned out OK:
I made an Asciinema demonstrating the features I’ve implemented so far. I apologise for the length (nearly 15 minutes!), but I couldn’t think of a better way to explain what’s already there and how to use it:
https://asciinema.org/a/X6QMxb0m1PvtzBdJ204A3ELdc
I’d really like to write helpful documentation with examples, and polish the interface to make it useful and comfortable. However, I didn’t write it because I needed it, I just wanted to see if it was possible, so I don’t really know what kind of examples would be helpful, or what kind of interface would be comfortable.
Would anybody here find this plugin useful? What do you want to be able to do? How would you want it to work? What examples would you look for in the documentation?
Bonus round: scripting tricks
If you’re not interested in using this plugin, but you might be interested in writing your own, here’s some cool tricks I found along the way.
- the repl-buffer-input helper script
- It’s natural for Kakoune to send inputs by opening a FIFO, writing, and closing, but a traditional Unix pipeline shuts everything down when the upstream pipe is first closed.
- This script watches a FIFO for multiple writers and combines them all into one output; to actually shut down the pipeline you must delete the FIFO from disk (and wait a few seconds for the script to notice).
- It’d be easy enough to do this in pure C, and drop the Python dependency, but then you’d need to compile it on every system.
- Writing a Kakoune command to send a newline charater is harder than it sounds.