Indentation in snippets

Hi,

Say I’ve got a snippet (GitHub - occivink/kakoune-snippets: Snippet support for kakoune) like the following:

hook global BufCreate .*\.envrc$ %{
  set -add buffer snippets 'envrc with flake support' '^envrc' %{
    snippets-insert %{use flake . -j12

if [ -e .envrc-local ]; then
  source .envrc-local
fi}
  }
}

This gives the correct result, but the problem is that the indentation of the above looks really crappy. Is there some way to make it look better without getting extra whitespace in the result?

It might depend on how snippets-insert work, but escaping newline by adding \ at end of line might work.

Adding \ at the end of the lines only makes those backslashes appear in the expanded snippet.

Alright, got to test some options in kakoune and this is probably the best you’ll get.

hook global BufCreate .*\.envrc$ %{
	set -add buffer snippets 'envrc with flake support' '^envrc' %{
		snippets-insert %sh{cat <<- EOF
			use flake . -j12

			if [ -e .envrc-local ]; then
			  source .envrc-local
			fi
		EOF}
	}
}

Kakoune takes it’s strings literarly and there isn’t much you can do besides processing them to escape the indentation. But shell heredoc does filter out indendation when you use the <<- DELIMITER syntax instead of << DELIMITER. The caveat here is that you must use tab for indenting the kakoune script for it to work properly, but the snippet itself can use spaces for indentation.
For example the line with source .envrc-local has the base indentation with tabs and uses spaces for the snippet-local indentation.

1 Like

Good idea with the heredoc! Thanks for thinking of it.