Pipe `buffer-next` to kakoune session

If you start up Kakoune, you can run :buffer-next and it will switch buffers.

If you run :new, you get a new client running in a new window, and if you run :buffer-next there, the first window doesn’t switch buffers - :buffer-next must be run in the context of a particular client, and it only affects that client.

Likewise, you can run :eval -draft %{ buffer-next }, and nothing happens. The documentation for :eval says that the -draft option “runs the commands in a draft context”; Kakoune creates a new context, :buffer-next switches which buffer that context acts on, and then the context is immediately discarded, with no affect on the current client.

Unfortunately, there’s no official list of what contexts are available, or what actions are possible in which contexts. If you look at :doc expansions value-expansions you’ll see that most of the expansions are available globally (like %val{version}, the currently-running version of Kakoune), some are available for buffers and windows (like %val{buffile}, the full path of the file being edited), some are only available in windows (like %val{selections}, the currently-selected text), and some are even more restricted (like %val{count} which is only available in the right-hand side of a mapping). Those aren’t the only contexts available, but those are the kinds of things contexts can be.

To answer your question, I suspect buffer-next doesn’t work for kak -p because kak -p is running commands in a global context, much like kakrc. There’s no way for kak -p to display a buffer, so it can’t switch which buffer is being displayed. To make it work, you probably want to do something like:

echo 'eval -client SomeClient %{ buffer-next }' | kak -p <session_name>

…where SomeClient is the name of the client which should switch buffers. You can check the current name of a client with echo %val{client} (run in the context of the client in question, of course), or if you want something more deterministic, you can use :rename-client to set it however you like.

4 Likes