I wrote a Bash script to help me manage some symlinks. It's a wrapper around some Stow commands.

When I invoke it, I pass it two command-line arguments: a command (plug/unplug/replug) and a directory name (just its basename, not a path).

The script is called vlink, so an invocation would be, for example:

vlink plug foo

The directory name (foo in the above example) needs to be one of the subdirectories of a specific directory elsewhere on my system.

Since there are only three possible commands (plug/unplug/replug) and a specific set of possible directory names for the third argument, I'd like to provide some tab-completion for it.

I'm somewhat familiar with writing Bash completion scripts, but is there a way to provide completion commands for an executable that's not in my $PATH and is only intended to be run from its parent directory?

1 Answer 1


This might work:

_vlink () {
    case $COMP_CWORD in
        1) COMPREPLY=( $(compgen -W "plug unplug replug" "$2") ) ;;
        2) local IFS=$'\n'
            COMPREPLY=( $(cd /some/dir && compgen -d "$2") ) ;;

complete -F _vlink vlink

Replace /some/dir with the directory containing the subdirectories of interest. (I'm assuming your directory names don't have newlines in them.)

  • COMP_CWORD is the index of the word being completed (0 being the command name)
  • the second argument to the completion funciton is the word being completed (the first being the command name and the third being the previous word)

So, we use compgen:

  • for the first argument, to generate matching words from the list of words given with -W, and
  • for the second argument, to generate matching directory names from the relevant directory.
  • As somebody whose bash proficiency is a little flaky, In a use case like this how to you load the completion? I have wanted to make completions myself a few times before but I have never really figured out how to register them besides globally.
    – Marie
    Mar 26, 2018 at 14:55
  • @Marie complete -F <function-name> <command-name>, like in the example above. Completion is registered to command names, not to to paths, so depending on what you mean by "globally", this can only be done globally (for all commands with the same name)
    – muru
    Mar 26, 2018 at 14:59
  • 2
    That was kind of what i meant. So there is no self-contained way for a command to set up its own autocomplete?
    – Marie
    Mar 29, 2018 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .