Terminal inside kakoune (text-file-terminal)

I want to run my terminal inside kakoune, just for the text editing/scrolling controls.

An experiment, I’ll dogfood it to see if it’ll have any future or if it’s a dead end. I couldn’t find any prior art in this direction, I’m surprised.

I considered having a separate file for input, but that was also a bit of a burden to manage due to having to jump windows all the time. However, current solution is far from ideal too, since the cursor drops behind quite often due to async output in the fifo. Separate input buffer is probably better after all.

I’ll also consider hook buffer RawKey .* and send each keypress to the terminal and have it echo whatever comes out.

2 Likes

kakoune-repl-buffer is another way to run command-line tools inside a Kakoune buffer, however it doesn’t try to run things inside a PTY. This means that terminal apps like nmtui won’t work, and some apps like Python and sqlite3 require some massaging, but on the other hand it avoids the whole mess of things like handling Ctrl-C and resize events and all that terminal stuff.

It also doesn’t let you type directly into the buffer, but there is a repl-buffer-prompt command that assumes the last line of the buffer is a prompt, passes it to Kakoune’s :prompt command (so you can type a response) and when you hit Enter it sends that line to the running process and waits for another prompt, so it “feels like” you’re typing interactively.

Actually getting something working with a real PTY would let you run a many more useful applications, but I think making it work reliably would also require much more work.

Good luck!

1 Like

In my analysis of plan9, I also felt the need to try to implement the “win” command of “acme”, which creates a shell and connects a buffer to that shell. So text from the buffer is transmitted to the shell, and the result of the shell is inserted into the buffer.

I haven’t tried yet to replicate this in Kakoune so I am not sure if it can be easily implemented. But that sounds appealing and I would be glad to hear more about that if you happen to progress.

https://9p.io/sources/plan9/acme/bin/source/win/main.c

1 Like

I just tried the oil shell headless mode, and unfortunately it didn’t seem to hit the mark on what I need exactly.

However, it does have a nice feature where it tells you when a command completes, that could fix the issue with cursor dropping midway to output. Then again maybe that’s fixable on kakoune side, I think the -scroll should handle it but it seems sketch

I haven’t figured a way to turn the background pty (started with script and working through FIFOs) into an actual pty, yet. I tried this but just got permission errors GitHub - nelhage/reptyr: Reparent a running program to a new terminal (probably the ptrace issue the have on the readme)…

That is kind of what my repl-buffer plugin does, except that it doesn’t try to trap arbitrary input to the buffer, only text you send with the repl-buffer-send-text or repl-buffer-prompt commands.

My understanding is that Acme tracks the last position in the buffer read from the shell, and any text inserted after that point is sent to the shell when Enter is pressed. Keeping track of text added after the last-read position seemed like a lot of work (especially since text can be added out of order) but now that I actually write it out, maybe it wouldn’t be so bad.

It’s actually quite convenient to get autocompletions into this shell from other open buffers; I’ll have one for chatgpt and the suggestions it gives are useful autocompletes all around the place.