Strip spaces at beginning of line during send-text command

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

Hi Ben, here is the command and link for sed ‘s/^\s*//g’.

below is untested, but its a place to start experimenting with.

map global user 'r' -docstring "send-text ^\s+" \
  %{: send-text %sh{printf %s ${kak_selection} | sed 's/^\s*//g' }<ret>}

the above is a hack-able file that you can copy and rename it then place the file in your .config/kak/* path. Just change the define-command names so no clashing, check kak buffer *debug* for any issues.

If all else fails just head back here in a couple of days and post the next problem you have with it.

Have fun :wave:

How did you go with it @Benjamin_Neil? Is it all sorted and onto the next problem? If not shout loudly in this direction.

Cool bye :wave:.

I think this snippet was stripping more than just the leading whitespace at the begging of each line. I turned off the auto indent of the repl as a workaround, but I would like to get this working.

IIUC you want this to be preserved in this form

if a then
    b
else
    c

Even if you have many spaces before if

            if a then
                b
            else
                c

And not to become

if a then
b
else
c

If so, I think this shell command will do the trick:

echo "${kak_selection}" | head -1 | awk -F'[^ \t]' '{print length($1)}'); printf "%s" "${kak_selection}" | sed -E "s/^\s{$leading}//g"

It first counts the amount of spaces in the first line, then it strips that amount from each line beginning. May not be optimal if the first line has more spaces than the rest, e.g.:

    if a then
        b
    else
        c
print
    a

will be transformed to

if a then
    b
else
    c
print
a

So you might to add a loop to find the smallest leading amount.

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.