Autocomplete on 3rd level argument? #7

esampaio opened this Issue Oct 3, 2012 · 6 comments


None yet
5 participants

esampaio commented Oct 3, 2012

First thanks a lot for this, besides being a well thought out framework, it gives us that little push to organize our scripts and share them with the team.

Now for my issue, I'm struggling with the autocompletion, I can make it work for the first level like:

❯ sub mage <tab>
foo        bar 

But I cant make it work on the second level, this is what happens:

❯ sub mage foo <tab>
foo        bar 

When I expected:

❯ sub mage foo <tab>
baz        fubar 

I've organised the files like:


And put the --complete like this:

# Provide sub completions
if [ "$1" = "--complete" ]; then
    { echo foo; echo bar; } | sort | uniq

Thanks in advance for any help

When you type sub mage foo and hit Tab, you're running the command sub-mage --complete foo. You can check the value of "$2" inside your completion conditional and output a different set of completions there.

@sstephenson sstephenson closed this Oct 3, 2012


qrush commented Oct 3, 2012

We should probably have this as a documented example in the README :)

esampaio commented Oct 3, 2012

Great, thanks @sstephenson =]
And +1 on adding this to the README

esampaio commented Oct 3, 2012

I attempted that, and it still didn't work, I don't want to be a bother here, but is it possible to do this?
Here is my code:

# Provide sub completions
if [ "$1" = "--complete" ]; then  
  if [ "$2" = "foo" ] || [ "$2" = "bar" ]; then
    { echo baz; echo bla; } | sort | uniq
    { echo foo; echo bar; } | sort | uniq

I even tried adding:

echo $#

But it always shows 1 no matter how many parameters before the <tab>

Thanks in advance, again.

bradical commented Nov 9, 2012

Confirmed. Seems like params beyond the first are not being passed to the sub-commands during auto-complete. Verified by dumping:

echo "Params: $1 $2 $3" >&2

in the first line of my auto-complete conditional and then trying it to do auto-complete with multiple args:

sub command <TAB>
sub command arg1 <TAB>
sub command arg1 arg2 <TAB>

The params only ever contain --complete.

capotej commented Nov 28, 2012

Yep, can confirm also.

@qrush qrush reopened this Dec 7, 2012

jeffreyroberts added a commit to jeffreyroberts/sub that referenced this issue Apr 23, 2013

cofyc pushed a commit to cofyc/sub that referenced this issue Apr 29, 2014

Append all arguments to --complete
Fixes #7
(if using bash)

When bash-completion is invoked, all arguments are now
forwarded to the command, e.g.

sub command arg1 arg2 <tab>

now triggers:

sub --complete command arg1 arg2

instead of just:

sub --complete command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment