# Inserting new line literal into a buffer from expansion

**URL:** <https://discuss.kakoune.com/t/inserting-new-line-literal-into-a-buffer-from-expansion/1055>\
**Category:** Help\
**Created:** [June 3, 2020, 9:52pm UTC](https://discuss.kakoune.com/t/inserting-new-line-literal-into-a-buffer-from-expansion/1055 "2020-06-03T21:52:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prion](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/prion/32/404_2.png) [@prion](https://discuss.kakoune.com/u/prion)\
**Post date:** [June 3, 2020, 9:52pm UTC](https://discuss.kakoune.com/t/inserting-new-line-literal-into-a-buffer-from-expansion/1055/1 "2020-06-03T21:52:39Z")

</div>

I’ve been working on a proof of concept for working with Kakoune expansions that will work something like the `create-context` function in this example.

```auto
define-command confirm-file-deletion %{
    prompt "Confirm Deletion? (YES/NO)" %{
        # %var{text} holds the contents of the user prompt
        create-context %var{text} %{
            try %{
                # is the user input the string yes?
                exec _s(?i)^yes$<ret>
                delete-file
            }
            catch %{ try %{
                # is the user input the string no? Then exit function.
                exec _s(?i)^no$<ret>
            } catch %{
                # input was invalid, ask for input again
                confirm-file-deletion
            }
        }
    }
}

```

Essentially I want to be able to treat the output of %opt{}, %var{}, %reg{} and %sh{} blocks as if they were a temporary buffer (plus some extra stuff like selecting each str in a `declare-option str-list` automatically). The intent is be able to program some basic logic using Kakoune’s text editing primitives and without using the shell. One of the restrictions at the moment is getting the contents of something like `%var{selection}` or `%sh{find}` into a buffer the same way and while preserving new lines.

Does anyone know a way to achieve this? I’m currently using

```auto
exec "i%arg{0}"

```

inside my `create-context` function but because this prints new line literals and not `<ret>` everything is one one line. In an ideal world there would probably be something like `echo -to-buffer 'temporary-buffer' %arg{0}`.

---

<div class="post-metadata">

**Author:** ![andreyorst](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/andreyorst/32/29_2.png) [@andreyorst](https://discuss.kakoune.com/u/andreyorst)\
**Post date:** [June 4, 2020, 7:04am UTC](https://discuss.kakoune.com/t/inserting-new-line-literal-into-a-buffer-from-expansion/1055/2 "2020-06-04T07:04:53Z")

</div>

IIUC you’re trying to test user input. Why not to use shell expansion? Like this

```auto
define-command -docstring "Ask before repeating last command (with `.' (dot)) for next search match" \
query-repeat %{ try %{
    execute-keys n
    prompt "confirm? [yn]: " -on-change %{ execute-keys %sh{
        case ${kak_text} in
            (y) printf "%s\n" "<esc>.: query-repeat<ret>";;
            (n) printf "%s\n" "<esc>: query-repeat<ret>" ;;
            (*) ;;
        esac
    }} nop
} catch %{
    fail "no search pattern"
}}

```

Or do you want to create a temporary buffer with user input and make any Kakoune commands act upon it as if it was active buffer, without making it visible?

---

<div class="post-metadata">

**Author:** ![prion](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/prion/32/404_2.png) [@prion](https://discuss.kakoune.com/u/prion)\
**Post date:** [June 4, 2020, 10:01pm UTC](https://discuss.kakoune.com/t/inserting-new-line-literal-into-a-buffer-from-expansion/1055/3 "2020-06-04T22:01:02Z")

</div>

The snippet I posted was just an example of how someone might use the `create-context` command and it actually works just fine. I just want to be able to do more with it and for that I need to figure out how to insert a multi-line expansion into a buffer

I’m not using shell expansions because I was experimenting with different ways to implement logic inside of Kakoune and using it’s text editing abilities with the textual variables, options and other expansions seemed natural to me.
