Thursday, March 20, 2025

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

Thank you, Marc.

So, I removed the Smart Tabs plugin and added this to my .vimrc:

set listchars=eol:⏎,tab:>·,trail:-,space:␣,extends:>,precedes:<,nbsp:+
set ai
command ArgIndent y | put | s!\v^\s+\zs(\S+\s+).*!\=repeat(' ',1+len(submatch(1)))!
inoremap \\<cr> <c-o>:ArgIndent<cr>


Then I opened a BASH script an started typing:

#!/bin/bash⏎

f()⏎
{⏎
>·······something⏎
>·······another_thing⏎

>·······local␣one␣two␣three␣four␣\\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣five␣\\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣six⏎

>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣foo⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣bar⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣}⏎
>·······------------⏎

The only spaces I have typed manually are those before 'five'.
Everything else is just the result of pressing Enter.

As you see, 'foo' and 'bar' are not indented with just a tab (as they
should be) but align with 'six'.

Also, the closing brace of the function appears in some strange place,
instead of the first column. Pressing Enter after it does not send the
cursor to the first column, as it should.

The double backslash is somewhat laggy (and adds unnecessary characters).


I tried using the regular single backslash and the result is correct.
After pressing Enter after 'four', I had to delete the automatic second
tab and align with spaces, then everything worked correctly:

#!/bin/bash⏎

f()⏎
{⏎
>·······something⏎
>·······another_thing⏎

>·······local␣one␣two␣three␣four␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣five␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣six⏎

>·······foo⏎
>·······bar⏎
}⏎


However, when testing with the initial example I get this (again, I
manually delete the auto-inserted tabs and replace them with alignment):

#!/bin/bash⏎

f()⏎
{⏎
>·······local␣a=1␣\⏎
>·······␣␣␣␣␣␣b=2⏎

>·······cmd␣--option1␣"one"␣\⏎
>·······␣␣␣␣--option2␣"two"␣\⏎
>·······␣␣␣␣arg1␣\⏎
>·······␣␣␣␣arg2␣\⏎
>·······␣␣␣␣|␣cmd2␣--option3␣"three"␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣--option4␣"four"␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣arg3␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣arg4␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣arg5␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣|␣cmd3␣--option5␣"five"␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣--option6␣"six"␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣arg6␣\⏎
>·······␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣arg7⏎
>·······>·······␣␣}⏎
>·······>·······--⏎

Everything works fine till 'arg7'. Pressing Enter after it results in
misaligned closing brace and the Enter after the brace positions the
cursor incorrectly.


Is there a way to define such behaviour (pseudo code in layman terms):

if (Enter is pressed after a single backslash); then
go to new line
preserve the indentation and alignment of the previous line
add spaces to align to the second word in the previous line
# word: anything that is not white space
fi

How can I do this?

--
--
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/20250320185209.7173b041%40localhost.

No comments: