Snippet that automatically installs plug.kak

Here is a snippet of code to put in your kakrc file (before you source plug.kak) that automatically installs plug.kak. I have done some cursory testing and it seems to work well on my computer. Any feedback/questions/issues that anyone would like to share is certainly welcome!

1    nop %sh{
2        if [ ! -d "$kak_config/plugins/plug.kak" ]; then
3            mkdir -p "$kak_config/plugins"
4            git clone "$kak_config/plugins/plug.kak"
5        fi
6    }


(Line 1) To my knowledge, the %sh expansions need to be given to a command, so I used the command nop because I didn’t care about using any of the code’s output for anything. You can probably replace nop with echo -debug if you want its output in the debug buffer instead.

(Line 2) This checks if $kak_config/plugins/plug.kak is a directory, where $kak_config is Kakoune’s config directory. If it exists and is a directory, plug.kak is installed and there is nothing to do, otherwise it goes through the next steps.

(Line 3) This makes a plugins directory in case there isn’t one already. Not necessary, see edits for details.

(Line 4) This uses git to install plug.kak in the proper directory.

Edit: As was pointed out to me by Reddit user u/Sakashi_NNB on my Reddit post about this, Line 3 is not actually needed because git creates it for you when it clones plug.kak.


You could also stick that in a catch block after trying to source plug.kak
to avoid shelling out every time you open Kakoune:

try %{
  source "%val{config}/plugins/plug.kak/rc/plug.kak"
} catch %{ nop %sh{
  if [ ! -d "$kak_config/plugins/plug.kak" ]; then
    git clone "" "$kak_config/plugins/plug.kak"

This is a great contribution, but it makes it so that you have to restart Kakoune in order to source it after installing it. I think that a few minor alterations would remediate this, though:

declare-option -docstring "plug.kak's directory" str plugkakdir "%val{config}/plugins/plug.kak"
declare-option -docstring "plug.kak's main script" str plugkak "%opt{plugkakdir}/rc/plug.kak"

try %{
    source "%opt{plugkak}"
} catch %sh{
    if [ ! -d "$kak_opt_plugkakdir" ]; then
        git clone "$kak_opt_plugkakdir"
        echo "source '%opt{plugkak}"

This will make sure that plug.kak gets properly sourced even if it’s not initially installed. I also took the opportunity to get rid of some code duplication that was present in my original snippet.