is there a simple way to quickly navigate to the dirname of the current buffer, e.g. to open another file in the same folder? I found %val{buffile}, but TAB doesn’t seem to expand it.
Essentially something like vim’s %:h (which was already too verbose, but at least does TAB-complete).
<c-r>% pastes the current buffer name. <a-!> expands things like %val{buffile}
also works with shell blocks, for example map global user e ':e %sh{dirname $kak_buffile}<a-!>/'
You do need to quote the argument to cd in case it has spaces but everything “inside” a %{} block is treated as a single string. So %sh{} or %opt{} can have spaces in the result and it won’t get interpreted as different arguments, unlike $() in the shell. In other words what you need to handle any whitespace in the path is
'cd %sh{dirname "$kak_buffile"}'
or alternatively
'cd %sh{dirname $kak_quoted_buffile}'
Interesting, I’ve never run into the consequences of that distinction before. Like any sane person I just wrap everything in double quotes. I guess the behaviour is to prevent quotes in variables from messing with the “top level” quoting.
It gets complex because Kakoune has a client-server architecture, and each client (and the server) can have a different set of environment variables, a different current directory, etc. If you wanted to use the current client’s environment to source a script, you could use source %val{client_env_FNAME} but since sourcing a script can affect the entire session, using the server’s environment is probably the right thing to do.