Key mapping to send Kakoune to background

I’ve been using :make so far, but now I have a python script that takes user input. So I’d like to have key mapping that writes all buffers :wa then sends Kakoune to the background i.e. ctrl-z.

I tried map global normal ':wa<ret><c-z>' but it appears that <c-z> does nothing.

Is this possible or can someone help find another solution?

I’ve tried :exec <c-z> but nothing happens.

Also I though there some behaviour like vim :make that would send you back into Kakoune when the shell command finishes.

there are many ways to take on this, on one hand, depending on the input, you could pipe it in the definition of some of your targets, or pipe a file to it, or if you really want to send kak to the background you might want to use something like xdotools.

You could define a target like runwithfile that runs the program but takes the input of the file with an input pipe <

I just tested this python script

i = input()
print(i)

with this target

runwithfile:
	@python test.py < input

where input is a file that is consumed line by line with each call to input, like it was indeed user input, I think it would be a more interactive way to do things, while also keeping those sweet kakoune keys.

Don’t quote me on this but I think ctrl+z is a terminal emulator thing, which could explain why it does nothing from within kakoune itself, again, xdotools should work for this matter, making a command with a shell block that calls what you need

It’s a bit complicated because (unlike, say, Vim) Kakoune has a client-server architecture. You can create new clients by running :new inside Kakoune, or by running kak -c SESSIONNAME (where SESSIONNAME is the “1234” part of the “client0@[1234]” visible at the right-hand end of Kakoune’s status bar), while the server is started automatically and shuts down when the last client disconnects.

When you press <c-z> to suspend “Kakoune”, that keystroke is intercepted by the client, which suspends itself rather than sending the keystroke along to the server.

When you put <c-z> in a mapping, or do :exec <c-z>, that executes on the server which knows nothing about terminals or which clients might be using them.

As an actual practical solution, I typically have two terminals open - one for Kakoune, and another one to run interactive scripts or tests or browse documentation or whatever.

Alternatively, you could use the :terminal command to run your script, which launches a new terminal instead of suspending Kakoune inside the current one.

If you don’t mind doing something a bit more hacky, instead of :exec <c-z> you could do something like:

nop %sh{ kill -TSTP $kak_client_pid }

…which should send the same signal to the current client that <c-z> normally would. I’m not sure if Kakoune gets an opportunity to do the same cleanup/restore it does with <c-z> though, so it might make your interactive script a bit glitchy.

1 Like

The input wasn’t known before time, but thanks.

I just ended up mapping write all to user z so I could quickly do ,z<ctrl-z> without forgetting to save my program! Visual studio has this feature where it actually saves your changes before running. Amazing!

The problem is I forget to save before running. So I need something like

map global user ',' ':wa<ret>:make<ret>'

I’ve been finding :make to be a bit difficult with matplotlib since the output (execptions as well as print()) doesn’t appear until I close the matplotlib window which is a bit annoying when debugging.

So I think it might be worth supporting a command that does something like

nop %sh{ kill -TSTP $kak_client_pid }

which window are you talking about?

the matplotlib window, sorry

Don’t know a lot about matplotlib, but maybe there is a way to tell it to close instantly, or maybe to run headlessly, don’t know if that makes sense.

I have an event on the window that handles key strokes so closing instantly won’t work.

the problem is the matplotlib show() blocks the python script until the window is closed. Somehow that doesn’t matter if I run it in the terminal though???

You might have already come across this python - view and then close the figure automatically in matplotlib? - Stack Overflow, still think your case is more a matplotlib than a kakoune thing, maybe you can look for something like the issue I pasted there, or maybe parameterize your script to not show given an argument you would provide trough an special target in make, but I think the way to go would be looking further on how to instruct matplotlib in it’s behavior, otoh, it might capture keys(matplotlib) but you still can send signals to it with kill, maybe if you name the process you could pgrep my process | xargs kill -9, or whichever code that would suffice for the program to stop displaying and get going.