[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash completion and spaces



On Sat 24 Apr 2021 at 13:46:24 (+0700), Victor Sudakov wrote:
> David Wright wrote:
> > > > > 
> > > > > I have an example app which can be run only as "app3 -h test1 -s foo" or
> > > > > "app3 -h test2 -s bar". So I decided to provide it with a small manual
> > > > > completion for convenience.
> > > > > 
> > > > > [vas@test2 ~]$ ./list.sh 
> > > > > -h test1 -s foo
> > > > > -h test2 -s bar
> > > > > [vas@test2 ~]$ complete -C ./list.sh app3
> > > > > [vas@test2 ~]$
> > > > > 
> > > > > The result however is discouraging, the completion mechanism won't add
> > > > > whole lines of parameters, it's trying to split on spaces (here I press
> > > > > <TAB> several times:
> > > > > 
> > > > > [vas@test2 ~]$ app3 -h -h -h test
> > > > > 
> > > > > Can you please give a hint how to make it complete "app3" with either
> > > > > "-h test1 -s foo" or "-h test2 -s bar" as a whole?
> > > > > 
> > > > > I would not like to make all this too complicated, write complex
> > > > > completion funcions if possible. A static (-W) completion would be even
> > > > > better.
> > > > 
> > > > Perhaps:
> > > > 
> > > > alias app3a='app3 -h test1 -s foo'
> > > > alias app3b='app3 -h test2 -s bar'
> > > 
> > > No, not alias, I'd like to do it via bash completion. I may want to make
> > > it dynamic eventually, the problem is in the spaces.
> > 
> > It's claimed that you can overcome this, in
> > 
> > https://stackoverflow.com/questions/11070370/bash-completion-for-strings-with-spaces-and-punctuation
> 
> Yes, this describes my case, unfortunately it lacks an example. Maybe
> googling for COMP_WORDBREAKS will yield some examples. Thanks for the
> link.
> 
> > but I'd be balancing the possibility of side-effects with redefining Space
> > against the benefits of completing a few strings.
> 
> If COMP_WORDBREAKS cannot be redefined within one and only one
> completion, then perhaps the game is not worth the candle.
> 
> BTW on my current Debian system I don't see the space character in $COMP_WORDBREAKS.

As davidson showed, it is there. My only suggestion would be a study of
/usr/share/bash-completion/completions/{cpan2dist,udisksctl,mount.linux,git},
the four commands that manipulate COMP_WORDBREAKS, to see if there are
any hints. Not my cup of tea.

> > I also don't understand what you mean by "dynamic", particularly as
> > you wrote "A static (-W) completion" above. 
> 
> I actually meant that a simple example of static completion with spaces
> (if someone cares to provide it) would help me understand the idea, and
> I could use it in a dynamic completion later if I need to.
> 
> > You could always generate
> > lists of aliases dynamically, too.
> 
> Well, I can make bash regenerate a completion list every time I
> enter "app3<TAB>", probably it can be done with aliases as well?

I think aliases can only work at the level of the entire command line,
ie the aliases must have been defined by the time you start typing the
line (which is why you can't define and use an alias in the same line).

OTOH bash completion completes at a character-to-character level AIUI,
ie <TAB> always recalculates a fresh list (though you have to rubout
one character and press <TAB> again to actually complete with it).

Cheers,
David.


Reply to: