# Adding a newline character to send-text?

**URL:** <https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257>\
**Category:** Help\
**Created:** [September 10, 2020, 9:21am UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257 "2020-09-10T09:21:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 10, 2020, 9:21am UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/1 "2020-09-10T09:21:23Z")

</div>

I’m trying to send a command to the repl pane with `send-text mycommand`. But then I need to send a newline, and it looks like `send-text` does not recognize special characters such as `\n`. Am I missing something?

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [September 10, 2020, 10:26am UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/2 "2020-09-10T10:26:57Z")

</div>

`send-text` doesn’t take a command-line argument. The idea is that you select the expression (or function, or class, or whatever) that you want to evaluate, then run `send-text` to copy that text to the clipboard and paste it into the REPL window. It should automatically paste a newline as well, so the expression should be evaluated even if one wasn’t included in the selection.

---

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 10, 2020, 11:55am UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/3 "2020-09-10T11:55:02Z")

</div>

Are you sure about that ? `tmux-send-text`’s help says:

> tmux-send-text [text]: Send text(append new line) to the REPL pane. If no text is passed, then the selection is used.

And typing `send-text "foo bar"` does send “foo bar” to the REPL, but does not add a new line.

---

<div class="post-metadata">

**Author:** ![tototest99](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tototest99/32/629_2.png) [@tototest99](https://discuss.kakoune.com/u/tototest99)\
**Post date:** [September 10, 2020, 12:04pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/4 "2020-09-10T12:04:08Z")

</div>

Low effort response: you need to modify `send-text` found in `rc/windowing/repl` of the Kakoune repo. For example with OCaml REPL and `Kitty` terminal I have:

```auto
    if [$# -eq 0]; then
        text="$kak_selection;;\n"
    else
        text="$1;;\n"
    fi

```

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [September 10, 2020, 12:17pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/5 "2020-09-10T12:17:46Z")

</div>

Ohhh, `tmux-send-text`. I don’t use tmux, I was testing with `x11-send-text` which only sends the selection, never a command-line argument.

This seems like the sort of thing that probably should be standardised among the various REPL backends.

---

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 10, 2020, 3:33pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/6 "2020-09-10T15:33:47Z")

</div>

> [@Screwtapello](#):
>
> This seems like the sort of thing that probably should be standardised among the various REPL backends.

That would be a good thing, yes! I’ll check if I can fix the `tmux-send-text` command to match the description.

---

<div class="post-metadata">

**Author:** ![duncan](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/duncan/32/95_2.png) [@duncan](https://discuss.kakoune.com/u/duncan)\
**Post date:** [September 10, 2020, 4:35pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/7 "2020-09-10T16:35:52Z")

</div>

An earlier thread on the same topic might also help you out.

> [@Strip spaces at beginning of line during send-text command](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935):
>
> Hello, How can I strip the spaces at the beginning of all new lines in a $kak\_selection? The problem is, my repl auto indents all new lines, so essentially I am doubling my indent when I send text to the repl. Btw, I don’t want to change the indent in the actual editor, only the information I am sending to the repl. Thank you, Ben

Also you can do this from kak command line by just selecting some desired text to test.

Preserves NL character:  
`:echo %sh{echo "${kak_selection}" | sed 's/^[\t]*//g'}`

Removes NL character:  
`:echo %sh{echo ${kak_selection} | sed 's/^[\t]*//g'}`

Lets us know if you get it going. Bye.

---

<div class="post-metadata">

**Author:** ![kanashimia](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/kanashimia/32/587_2.png) [@kanashimia](https://discuss.kakoune.com/u/kanashimia)\
**Post date:** [September 10, 2020, 10:14pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/8 "2020-09-10T22:14:06Z")

</div>

to input newline - you need to use raw insert `<c-v>` and press enter  
so `send-text command<c-v><ret> <ret>`

you can map command to some key  
`map buffer user <0> %{ :send-text "ls<c-v><ret>" <ret>}`

or define wrapper command that adds newline  
`define-command snd -params 1 %{ send-text "%arg{1}␊" }`  
notice LF before quote here, that’s how it looks after using `<c-v>`

---

<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:** [September 10, 2020, 11:44pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/9 "2020-09-10T23:44:35Z")

</div>

I didn’t know about the `<c-v><ret>` trick to insert a ␤ into the command input. I have always written the equivalent command using a literal newline, for example my GHCI REPL wrapper is something along the lines of

```auto
define-command ghci-send-text %{
    # strip any extra whitespace, need to add an extra new line to get repl to eval.
    exec Z_
    try %{
        # for multi-line commands
        exec -draft "s\n<ret>"
        send-text ":{
%val{selection}
:}
"
    } catch %{
        send-text "%val{selection}
"
    }
    exec z
}

```

---

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 11, 2020, 1:02pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/10 "2020-09-11T13:02:37Z")

</div>

Wow, it took me some time to understand I was supposed to actually type “ctrl-v \<ret\>” in order to insert a LF character 😅

But still, when I run kak inside tmux, open a terminal with `:repl` and run the command `:send-text ls<c-v><ret>`, all I get is “ls” appearing in the repl, but no newline entered … Am I being dense?

---

<div class="post-metadata">

**Author:** ![useredsa](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/useredsa/32/419_2.png) [@useredsa](https://discuss.kakoune.com/u/useredsa)\
**Post date:** [September 11, 2020, 1:42pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/11 "2020-09-11T13:42:14Z")

</div>

I suppose you need to quote the parameter.

---

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 11, 2020, 1:52pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/12 "2020-09-11T13:52:16Z")

</div>

Here we go again. After looking at `tmux-send-text`'s definition:

```auto
define-command -hidden tmux-send-text -params 0..1 -docstring %{
        tmux-send-text [text]: Send text(append new line) to the REPL pane.
        If no text is passed, then the selection is used
    } %{
    nop %sh{
        if [$# -eq 0]; then
            tmux set-buffer -b kak_selection "${kak_selection}"
        else
            tmux set-buffer -b kak_selection "$1"
        fi
        tmux paste-buffer -b kak_selection -t "$kak_opt_tmux_repl_id"
    }
}

```

Let’s try this without kakoune, using tmux set-buffer and paste-buffer

```auto
<c-b>:set-buffer -b foo "ls\n"<ret>
<c-b>:paste-buffer -b foo<ret>
$> ls
[my files here]>

```

behold! passing `\n` to the tmux command works. Now let’s define a command in my kakrc

```auto
define-command send-ls %{
    nop %sh{
        #tmux set-buffer -b test "ls\n" #won't work, will send the string \n
        tmux set-buffer -b test "ls
"
#this is ugly, but the \n character is sent.
        tmux paste-buffer -b test -t "$kak_opt_tmux_repl_id"
    }
}

```

Well, I guess I have a solution now, but I feel like I’m bending backwards to send a newline character …

---

<div class="post-metadata">

**Author:** ![tgirod](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tgirod](https://discuss.kakoune.com/u/tgirod)\
**Post date:** [September 11, 2020, 1:55pm UTC](https://discuss.kakoune.com/t/adding-a-newline-character-to-send-text/1257/13 "2020-09-11T13:55:00Z")

</div>

You are right, from kak’s command line, `:send-text "ls<c-v><ret>"` works!

EDIT: And from a command defined in kakrc too, but some invisible character got me …
