# Indentation in snippets

**URL:** <https://discuss.kakoune.com/t/indentation-in-snippets/2405>\
**Category:** Help\
**Tags:** format, plugins\
**Created:** [February 17, 2024, 10:07am UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405 "2024-02-17T10:07:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joncol](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/joncol/32/1192_2.png) [@joncol](https://discuss.kakoune.com/u/joncol)\
**Post date:** [February 17, 2024, 10:07am UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405/1 "2024-02-17T10:07:55Z")

</div>

Hi,

Say I’ve got a snippet ([GitHub - occivink/kakoune-snippets: Snippet support for kakoune](https://github.com/occivink/kakoune-snippets)) like the following:

```auto
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?

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [February 17, 2024, 12:38pm UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405/2 "2024-02-17T12:38:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joncol](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/joncol/32/1192_2.png) [@joncol](https://discuss.kakoune.com/u/joncol)\
**Post date:** [February 19, 2024, 11:22am UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405/3 "2024-02-19T11:22:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [February 19, 2024, 12:08pm UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405/4 "2024-02-19T12:08:45Z")

</div>

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

```sh
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.

---

<div class="post-metadata">

**Author:** ![joncol](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/joncol/32/1192_2.png) [@joncol](https://discuss.kakoune.com/u/joncol)\
**Post date:** [February 19, 2024, 3:47pm UTC](https://discuss.kakoune.com/t/indentation-in-snippets/2405/5 "2024-02-19T15:47:36Z")

</div>

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