Add argument chain to --complete to make multiple level compeltion possible #12

xCette opened this Issue Oct 8, 2012 · 4 comments


None yet

5 participants


I want to be able to tab complete the list of servers in this example:
sub setup setupServers (foo, bar baz)

I dont see any easy way to do this, but I cold be wrong. If the list of completed arguments was passed to --complete then I would be able to parse down them and return completions to an arbitrary depth.



The following patch solves the issue:

diff --git a/completions/sub.bash b/completions/sub.bash
index 40eac81..37c210c 100644
--- a/completions/sub.bash
+++ b/completions/sub.bash
@@ -6,7 +6,7 @@ _sub() {
     COMPREPLY=( $(compgen -W "$(sub commands)" -- "$word") )
     local command="${COMP_WORDS[1]}"
-    local completions="$(sub completions "$command")"
+    local completions="$(sub completions "$command" ${COMP_WORDS[@]:2})"
     COMPREPLY=( $(compgen -W "$completions" -- "$word") )

This is awesome and very powerful. Thanks so much. I would have done it myself by I got lost after the 2nd or 3rd line of bash scripting :)

Is there any way the script could more gracefully handle a tree like structure? Like if sub-server supplied (foo, bar, baz) and tab completing "sub server bar" automatically looked at sub-server-bar first for completions. That certianly requires some careful thought the effects of that change, but it would make building a robust command structure much easier.

@xCette xCette closed this Oct 9, 2012
@qrush qrush reopened this Oct 9, 2012

This might be similar but I'm looking to provide multiple levels of auto-completion for a server name for the first argument and a user for the second:

sub ssh host user

Currently trying to accomplish with:

if [ "$1" = "--complete" ]; then
  exec echo "server1 server2 server3"

if [ "$2" = "--complete" ]; then
  exec echo "ec2-user root"

Is this the same idea?

@chrisjaure chrisjaure added a commit to chrisjaure/sub that referenced this issue Dec 4, 2012
@chrisjaure chrisjaure fall through to default bash completion
When there are no completions for a command, fall through to default bash completion. Fixes #16.

I also added the patch from #12.

This seems to do the trick for zsh. Though I'm still getting stumped on how to write a script that handles both partial completions (e.g., first letter) and multi level completions.

Seems like we'll need something at the bash/zsh level to know if it's a full word coming in but I haven't figured it out yet.

diff --git a/completions/nf.zsh b/completions/nf.zsh
index 01aef31..0b16dbf 100644
--- a/completions/nf.zsh
+++ b/completions/nf.zsh
@@ -12,7 +12,7 @@ _nf() {
   if [ "${#words}" -eq 2 ]; then
     completions="$(nf commands)"
-    completions="$(nf completions "${word}")"
+    completions="$(nf completions "${word}" ${words[3,-1]})"

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