Ubuntu: How to create script with auto-complete?


When I use program like svn and I type in Gnome Terminal:

svn upd  

and hit Tab it's autocompleted to:

svn update  

Is it possible to do something like that in my custom bash script?


You can use the Programmable Completion. Have look at /etc/bash_completion and /etc/bash_completion.d/* for some examples.


I'm six months late but I was looking for the same thing and found this:

You'll have to create a new file:


For a static autocompletion (--help / --verbose for instance) add this:

_foo()   {      local cur prev opts      COMPREPLY=()      cur="${COMP_WORDS[COMP_CWORD]}"      prev="${COMP_WORDS[COMP_CWORD-1]}"      opts="--help --verbose --version"        if [[ ${cur} == -* ]] ; then          COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )          return 0      fi  }  complete -F _foo foo  
  • COMP_WORDS is an array containing all individual words in the current command line.
  • COMP_CWORD is an index of the word containing the current cursor position.
  • COMPREPLY is an array variable from which Bash reads the possible completions.

    compgen -W "--help --verbose --version" -- "<userinput>"

This command returns the array of elements from --help, --verbose and --version matching the current word "${cur}"

Source : http://www.debian-administration.org/articles/316


Here is a complete tutorial.

Lets have an example of script called admin.sh to which you would like to have autocomplete working.

#!/bin/bash    while [ $# -gt 0 ]; do    arg=$1    case $arg in      option_1)       # do_option_1      ;;      option_2)       # do_option_1      ;;      shortlist)        echo option_1 option_2 shortlist      ;;      *)       echo Wrong option      ;;    esac    shift  done  

Note option shortlist. Calling script with this option will print out all possible options for this script.

And here you have the autocomplete script:

_script()  {    _script_commands=$(/path/to/your/script.sh shortlist)      local cur    COMPREPLY=()    cur="${COMP_WORDS[COMP_CWORD]}"    COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )      return 0  }  complete -o nospace -F _script ./admin.sh  

Note that the last argument to complete is the name of the script you want to add autocompletion to. All you need to do is to add your autocomplete script to bashrc as

source /path/to/your/autocomplete.sh  

or copy it to /etc/bash.completion.d


If all you want is a simple word based auto-completion (so no subcommand completion or anything), the complete command has a -W option that just does the right thing.

For example, I have the following lines in my .bashrc to autocomplete a program called jupyter:

# gleaned from `jupyter --help`  _jupyter_options='console qtconsole notebook' # shortened for this answer  complete -W "${_jupyter_options}" 'jupyter'  

Now jupyter <TAB> <TAB> autocompletes for me.

The docs at gnu.org are helpful.

It does seem to rely on the IFS variable being set correctly, but that hasn't caused any issues for me.

To add filename completion and default BASH completion, use the -o option:

complete -W "${_jupyter_options}" -o bashdefault -o default 'jupyter'  

To use this in zsh, add the following code before running the complete command in your ~/.zshrc:

# make zsh emulate bash if necessary  if [[ -n "$ZSH_VERSION" ]]; then      autoload bashcompinit      bashcompinit  fi  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »