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