I have been looking around the kakoune website for any documention on the language / details of writing plugins for this editor. Could someone kindly point me in the right direction. Not fond of the idea of having to look at existing plugins written to reverse engineer how it works. Is there an api?
Thereâs not a lot of documentation on plugin development because thereâs not a lot in Kakoune thatâs specific to plugins.
A plugin is just a file with the .kak
extension, containing the same commands youâd type at the :
prompt, so youâll first want to investigate :doc commands
. The two most important commands, :evaluate-commands
and :execute-keys
, have a lot in common and donât fit neatly into the regular commands documentation, so theyâre documented separately under :doc execeval
.
Scripting actual editing operations is done by giving :execute-keys
a sequence of normal-mode commands, most of which you probably already know from using Kakoune interactively, but thereâs a lot of lesser-known keys that come in handy for scripting, so youâll want to read :doc keys
too.
Often while scripting youâll want to examine some part of Kakouneâs state, like âwhat are the coordinates of the current selectionâ, or âwhat arguments were given to this functionâ, or âwhatâs the current value of this optionâ. For this kind of thing, youâll need to read :doc expansions
. In particular, note the %sh{}
shell-script expansion â itâs the only sensible way to do things like conditions or loops. Itâs a pretty common idiom to invoke :evaluate-commands
with a shell-expansion that generates the commands to be evaluated, like this:
evaluate-commands %sh{
case "$kak_selection" in
*x*)
echo info -anchor "$kak_cursor_line.$kak_cursor_column" \
selection contains an x
;;
*)
echo info -anchor "$kak_cursor_line.$kak_cursor_column" \
selection does not contain x
;;
esac
}
One part of Kakoune that technically can be used interactively but almost never is, is the âhookâ system, which lets you run commands when some particular event occurs. Check out :doc hooks
, and because hooks are installed in scopes youâll want to read :doc scopes
too.
Apart from that, learning Kakoune scripting is really just about learning how to combine these somewhat-unusually-shaped puzzle-pieces to create the particular shape you want. For that, poking at existing plugins that do something similar is a good way to learn. Note that Kakoune plugins can do a lot with only a little code, so it probably wonât be as daunting as you expect.
Remember, if you hit something weird you can always ask more questions here, or drop by the IRC channel!
@Screwtapello I genuinely thank you for this perfect answer. This explains what I need to know and where to find more pertinent information. While waiting for an answer I did happen to stumble upon :doc execeval
as well I noticed that evaluate-commands %sh
was popularly used. I wonder though if I can use zsh scripting in the same %sh
block. I know python but if doing stuff like if statements, conditionals and that sort of thing, I would rather learn some zsh as opposed to sh though I honestly have not investigated this I will do so now to see what the differences are. I will be reading each of those documentions you pointed out and I feel that what you have provided is a complete answer. Thank you.
I have not been using kakoune, I have previously been using vim and was looking for other options. It was a toss-up between Kakoune and Vis both of which I am up to the challenge of learning. It seems now my answer is Kakoune because of the documentation, and the scripting versus using lua for Vis. However, I do wonder if there is an option to debug kak scripts.
Much appreciation.
:echo -debug 'my debug message'
will print âmy debug messageâ to the debug buffer which you can edit with
:b *debug*
you can also just send messages to a scratch buffer or append it to a file by echoing it from a shell.
Thereâs a couple of options here:
- zsh is a command-line tool just like awk or sed or python, so you can run it inside a
%sh{}
block quite happily - zsh is (mostly, I think) an expanded and extended implementation of POSIX shell, so basic things like if statements work exactly the same way â learning POSIX shell is learning (part of) zsh, much like learning C is learning (part of) C++
- If you set the
$KAKOUNE_POSIX_SHELL
environment variable to point at zsh before starting Kakoune, it will run every%sh{}
block through zsh⌠but Iâm not sure zsh is 100% compatible with basic POSIX shell, so I canât guarantee Kakoune would still work correctly afterward
As well as :echo -debug
that prion mentioned, the stderr of any %sh{}
block gets sent to the *debug*
buffer, so when things donât work as you expect that can be very helpful.
Thank you both. I am very greatful for the answers provided and feel that I am ready to start using Kakoune. I feel prepared to write Kakoune scripts and have the documentation and tools (evaluate-commands
, expansions
, echo
and the *debug*
buffer) to handle any use case I encounter not already covered by the bundled autoload scripts and user contributed plugins / extensions. Thank you also for being not only very though and knowledgeable but also welcoming to a newcomer.
thereâs also this document that has a little info and general ideas behind scriptin Kakoune
Just in reply to Andreyâs
I found the link bellow solved a missing piece of the puzzle for me when I first started with Kakoune. An Illustrated Redirection Tutorial.