5

When I create a command that wraps an existing command with some sugar, I'd like the new command to support the autocompletion of the original command. Is there a way to tell Bash to reuse the autocompletion script of another command?

Silly example:

cat > ~/ls-on-steroids.sh <<EOF
echo "Here are some goodies!"
ls "$@"
EOF
chmod +x ~/ls-on-steroids.sh

Now, how do I configure my new script such that when I type:

~/ls-on-steroids.sh <TAB><TAB>

I'd like the same behavior as with:

ls <TAB><TAB>

Preferably in a portable, repeatable manner, without having to manually track down the location of ls's autocomplete script.

2 Answers 2

6

You have to configure it manually, but it's relatively simple to copy completions from an existing command. First, run complete -p ls to see what, if any, command was defined for ls. If nothing comes up, ls doesn't use any special completions. You are probably expecting to see something like the following as the output, though

complete -o default -F _longopt ls

which says that the function _longopt is called to generate completions for the command ls, and if that doesn't generate any results, use the bash default completion. You can apply the same function to ls_on_steroids by simply running

complete -o default -F _longopt ls_on_steroids

(i.e., replace ls with ls_on_steroids as the final argument in the command printed by complete -p).

Sign up to request clarification or add additional context in comments.

5 Comments

That sounds like it could be automated. Any caveats though? Can that command look different on different systems? Is there a more direct way to get the name of the command that serves the autocomplete?
It looks like complete takes multiple commands, so I can just run: complete -p ls | sed 's/$/ ls-on-steroids/ to include my script in ls's autocompletion.
I meant: $(complete -p ls | sed 's/$' ls-on-steroids/')
Most of the completions that come with bash_completion are lazy loaded (ls and cd are not but tmux, ssh, make, and many more are). If you start a new shell, complete -p tmux might print "bash: complete: tmux: no completion specification" but if you then type tmux <tab><tab> and do it again, you will see complete -F _tmux tmux.
@PhilippeCarphin You also need to make sure that the lazily-loaded completions are loaded for your command. See this question
1

Since I had to combine this question/answer and the other linked to in the comments in order to use Bash autocompletion of another command, I just wanted to summarize:

Content in ~/.bashrc or ~/.bash_aliases file:

# load completions for rsync:
shopt -s extglob; source /usr/share/bash-completion/completions/rsync

# apply same completion to my function:
complete -o nospace -F _rsync lsync

# define my function
function lsync {
  rsync --my-selection-of-flags "$@"
  discord-bot "rsync complete"  # notify me when done
}

Now I can happily lsync server: TabTab to view contents on the server, or lsync -- TabTab to view available rsync flags.

1 Comment

LOL is that lsync as in left sync versus right sync?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.