wiki/help/linux/bash/basic/autocomplete.md

3 KiB

Bash autocomplete basic guide.

Here is basic guide.

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

#!/usr/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.sh script:

#!/usr/bin/bash

_script() {
  _script_commands=$(/path/to/your/admin.sh shortlist)

  local cur prev
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )
  return 0
}

complete -o nospace -F _script /path/to/your/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 or /etc/bash_completion.d as:

source /full-path/to/your/autocomplete.sh

# or

. /full-path/to/your/autocomplete.sh

Finally, make them executable:

chmod a+x admin.sh autocomplete.sh

Source: https://askubuntu.com/a/483149/24155

source

My own autocomplete functions.

# bash autocomplete.
# usage: _foo () { _autocomplete "{foo,bar}" } ; complete -F _foo foo
# there are also options like -o nospace. see man for more info.
_autocomplete()
{
  local commands="$@"
  local cur prev
  
  COMPREPLY=()

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  command="${COMP_WORDS[0]}"

  COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
  return 0
}

# autocomplete only first argument.
_autocomplete_first()
{
  local commands="$@"
  local cur prev
  
  COMPREPLY=()

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  command="${COMP_WORDS[0]}"

  if [[ "${prev}" = "${command}" ]]; then
    COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
    return 0
  fi
}

# autocomplete only first argument. the rest is ls output.
_autocomplete_first_ls()
{
  local commands="$@"
  local cur prev
  
  COMPREPLY=()

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  command="${COMP_WORDS[0]}"

  if [[ "${prev}" = "${command}" ]]; then
    COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
    return 0
  else
    COMPREPLY=( $(compgen -W "$(ls -a)" -- ${cur}) )
    return 0
  fi
}

# autocomplete nested program.
_autocomplete_nested()
{
  local cur prev words cword split i
  _init_completion -s || return

  for ((i = 1; i <= cword; i++)); do
    if [[ ${words[i]} != -* ]]; then
      local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
      local root_command=${words[i]}
      _command_offset $i
      return
    fi
  done
}

sample usage:

# autocomplete.
_tb_containers()
{
  _autocomplete "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')"
}

complete -F _tb_containers tb tbk tb_rpmfusion