Last Updated: February 25, 2016
· adziendziel

Add bash-like autocompletion to Drush5

Drush has a dozens of commands and these commands have their own parameters... not everybody has ambitions to win competitions from knowledge of the drush commands.
Fortunately, we are able to enable autocomplete and then list and choose commands/parameters by pressing [TAB] key.

Copy following code and past it at the end of ~/.bashrc file

# Ensure drush is available.
which drush > /dev/null || alias drush &> /dev/null || return

__drush_ps1() {
  if [ -f $f ]
    DRUPAL_SITE=$(cat "$f")

  [[ -n "$DRUPAL_SITE" ]] && printf "${1:- (%s)}" "$DRUPAL_SITE"

# Completion function, uses the "drush complete" command to retrieve
# completions for a specific command line COMP_WORDS.
_drush_completion() {
  # Set IFS to newline (locally), since we only use newline separators, and
  # need to retain spaces (or not) after completions.
  local IFS=$'\n'
  # The '< /dev/null' is a work around for a bug in php libedit stdin handling.
  # Note that libedit in place of libreadline in some distributions. See:
  COMPREPLY=( $(drush --early=includes/ "${COMP_WORDS[@]}" < /dev/null 2> /dev/null) )

# Register our completion function. We include common short aliases for Drush.
complete -o nospace -F _drush_completion d dr drush drush5 drush6 drush6 drush.php

Save the file and reload bash:

source ~/.bashrc

Note: This code is copy from file which You can find here:

2 Responses
Add your response

You can also use the script directly fron your Drush installation, simply add the following to either your ~/.bashcompletion or ~/.bashprofile file:

source /path/to/drush/
over 1 year ago ·

Awsome! Looking for something like this for a long time!

over 1 year ago ·