Adding a newline character to send-text?

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?

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.

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.

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:

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

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.

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

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

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.

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>

1 Like

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

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
}

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

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?

I suppose you need to quote the parameter.

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

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

<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

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 …

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 …