# Strip spaces at beginning of line during send-text command

**URL:** <https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935>\
**Category:** Help\
**Created:** [March 27, 2020, 3:01pm UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935 "2020-03-27T15:01:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Neil](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/benjamin_neil/32/449_2.png) [@Benjamin\_Neil](https://discuss.kakoune.com/u/Benjamin_Neil)\
**Post date:** [March 27, 2020, 3:01pm UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/1 "2020-03-27T15:01:56Z")

</div>

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

---

<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:** [March 27, 2020, 6:24pm UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/2 "2020-03-27T18:24:50Z")

</div>

Hi Ben, here is the command and link for sed [‘s/^\s\*//g’](https://sed.js.org/?gist=122df5c2f541ae2da46f5d20ef60bc21).

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

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

```

* * *

> [@Kakoune tmux-repl usage](https://discuss.kakoune.com/t/kakoune-tmux-repl-usage/923/5):
>
> the use of kak’s [`:send-text`](https://github.com/mawww/kakoune/blob/master/rc/windowing/repl/tmux.kak#L39) command to do the copy and paste.

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 👋

---

<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:** [April 11, 2020, 7:52am UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/3 "2020-04-11T07:52:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Benjamin\_Neil](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/benjamin_neil/32/449_2.png) [@Benjamin\_Neil](https://discuss.kakoune.com/u/Benjamin_Neil)\
**Post date:** [April 14, 2020, 3:04am UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/4 "2020-04-14T03:04:08Z")

</div>

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.

---

<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:** [April 14, 2020, 7:50am UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/5 "2020-04-14T07:50:50Z")

</div>

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.

---

<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:** [April 16, 2020, 12:25am UTC](https://discuss.kakoune.com/t/strip-spaces-at-beginning-of-line-during-send-text-command/935/6 "2020-04-16T00:25:32Z")

</div>

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.
