Friday, March 21, 2025

Re: BASH script: Indent with tabs, align with spaces

Hi Steven,

answering the last question seems to be a good way to put all together
in a simpler way so let's back on your clever pseudocode, I'll add
comments on this.

Short anwser is:

* ArgIndent does exactly what your pseudocode expects
* you need *both* 'ci and 'ai to be turned on
* CTRL_D and CTRL_T is the way to increment/decrement manually
in insert mode. >> and << in normal mode.

Long anwser: inoremap \<cr> to ArgIndent does exactly what your
pseudocode want but in a different way

> if (Enter is pressed after a single backslash)

=> inoremap \<cr>

> preserve the indentation and alignment of the previous line
=> 'ai and 'ci must be set

> * go to new line
> * add spaces to align to the second word in the previous line
> # word: anything that is not white space

let's rewrite ArgIndent in vim9script and add comments

command -nargs=0 ArgIndent {
y | put # yyp in normal mode # stage 1
s!\v^\s+\zs(\S+\s+).*!\=repeat(' ', 1 + len(submatch(1)))!
}

at the end of the stage 1, we have

this --example \
this --example \

and we want to remove 'this' which is a substitution from
this

\v^\s+\zs(\S+\s+).*

to this

\=repeat(' ', 1 + len(submatch(1)))

in details:

\v # use "perlish/extended" regexp syntax
^ # the begin of the line
\s+ # at least one space|tab|nbsp
\zs # start matching there
# so previous chars will remain untouched
(\S+\s+) # capture a word "\S+" and the spaces after "\s+"
. # then the rest of the line

so in
this --example \

* 'this' is the word (submatch(1))
* '--example \' is the rest of the line

so the matched section is 'this --example \' and we replace it by
as many spaces as chars in in the word + 1

1 + len(submatch(1))
=> 1 + len("this")
=> 1 + 4
=> 5

repeat(' ', 5 )
=> ' '

so

this --example \

becomes

--example \

don't hesitate to ask if I wasn't clear.

regards


--
Marc Chantreux
Pôle CESAR (Calcul et services avancés à la recherche)
Université de Strasbourg
14 rue René Descartes,
BP 80010, 67084 STRASBOURG CEDEX
03.68.85.60.79

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/Z90Yb_gGzIXOCAdd%40prometheus.

No comments: