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

Re: bash completion and spaces



On Sat, 24 Apr 2021 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?

Almost entirely idle curiosity: the arguments test1/test2 and foo/bar
which your app3 requires, are they arbitrary strings?  Or are they
instead paths to files, or perhaps one of the other 20-odd categories
recognised by the -A option for bash builtin "complete"?

I would not like to make all this too complicated, write complex
completion funcions if possible.

In the case you describe, you might find the behaviour of readline
function "menu-complete" slightly more helpful than the readline
function "complete" presently mapped to TAB (given that you find the
behaviour of "complete" completely unhelpful, naturally enough).

See readline(3) for some documentation.

You could, say, map Shift-TAB to "menu-complete" in ~/.inputrc

(In emacs to get Shift-TAB to produce a literal Shift-TAB, you'll need
to hit Ctrl-q first.)

I make this suggestion as a partial workaround/improvement to present
situation. I doubt you'll find it an ideal solution.

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.

If you have xxd installed, what does xxd show you?

 $ printf "%s"  "$COMP_WORDBREAKS" | xxd

If you do not have xxd installed, what does od show you?

 $ printf "%s"  "$COMP_WORDBREAKS" | od -A x -t x1z -v

Anyways, good luck. I think this is an interesting problem.

--
Ce qui est important est rarement urgent
et ce qui est urgent est rarement important
-- Dwight David Eisenhower


Reply to: