Indenting shell code inside a kakrc file seems impossible

For example, kak doesn’t indent the following code properly.

evaluate-commands %sh{
  if [ $foo ]; then
    echo ok
  fi
}

When you say “kak doesn’t indent”, you mean Kakoune’s automatic-indent hook (when you press Enter) doesn’t produce that indentation by default? Or do you mean you can’t figure out how to achieve that indentation manually, or you can but it breaks the shell-script, or something else?

If I manually type the code in a kakrc file, I get this indentation.

evaluate-commands %sh{
  if [ $foo ]; then
  echo ok
  fi
}

I want enter key to indent shell code properly. By the way, I’m using auto-pairs.kak

The indentation logic for Kakoune files is pretty simple, basically indenting for %whatever and dedenting for block-closing.

The indentation logic for shell files is much more complex, and tries to handle all the different compound statements the shell provides, and many common indentation conventions.

Unfortunately, it’s not easy to have indentation hooks restricted to a particular region of a buffer, the way that shell highlighting can be restricted to %sh{} blocks. It’s probably possible to concatenate the Kakoune and shell indentation functions somehow, and since Kakoune syntax doesn’t do anything in the shell and shell syntax doesn’t do much in Kakoune it’d probably work OK—but shell isn’t the only language that appears inside Kakoune files: I’ve put Python, AWK, and SQL in Kakoune plugins, and other people have used even wilder languages.

That’s not to say that shell-indenting shouldn’t be in Kakoune files; after all, it’s the only scripting language with built-in support (in the form of %sh{} blocks). But so far, it hasn’t been important enough to motivate anybody to figure out how to combine those two indentation hooks.

Until sh indentation is implemented in kakrc files, should I just manually switch filetype?

You certainly could!

You could also experiment with combining those indentation hooks yourself; nobody else is working on that as far as I know, and the fragments I linked to should have all the pieces you’d need. If you get stuck, feel free to post questions here, or on the IRC channel or the Discord.