Add bash-like autocompletion to Drush5

Public

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 and then run:
source ~/.bashrc

Note: This code is copied from drush.complete.sh file which You can find here:
http://drupalcode.org/project/drush.git/blob/HEAD:/drush.complete.sh

</> CopyGet raw version
bash
  1. # Ensure drush is available.
  2. which drush > /dev/null || alias drush &> /dev/null || return
  3.  
  4. __drush_ps1() {
  5. f="${TMPDIR:-/tmp/}/drush-env/drush-drupal-site-$$"
  6. if [ -f $f ]
  7. then
  8. DRUPAL_SITE=$(cat "$f")
  9. fi
  10.  
  11. [[ -n "$DRUPAL_SITE" ]] && printf "${1:- (%s)}" "$DRUPAL_SITE"
  12. }
  13.  
  14. # Completion function, uses the "drush complete" command to retrieve
  15. # completions for a specific command line COMP_WORDS.
  16. _drush_completion() {
  17. # Set IFS to newline (locally), since we only use newline separators, and
  18. # need to retain spaces (or not) after completions.
  19. local IFS=$'\n'
  20. # The '< /dev/null' is a work around for a bug in php libedit stdin handling.
  21. # Note that libedit in place of libreadline in some distributions. See:
  22. # https://bugs.launchpad.net/ubuntu/+source/php5/+bug/322214
  23. COMPREPLY=( $(drush --early=includes/complete.inc "${COMP_WORDS[@]}" < /dev/null 2> /dev/null) )
  24. }
  25.  
  26. # Register our completion function. We include common short aliases for Drush.
  27. complete -o nospace -F _drush_completion d dr drush drush5 drush6 drush6 drush.php

Comments

sibiru's picture

Hi, I have error when execute source ~/.bashrc

  1. -bash: /root/.bashrc: line 102: syntax error near unexpected token `('
  2. -bash: /root/.bashrc: line 102: `which drush > /dev/null || alias drush &> /dev/null || return __drush_ps1() {'
zsh's picture

does this autocomplete work on zsh?

i've fount oh my drush zsh but not work for project auto complete

Sergej's picture

It works, thanks :)
Is there a way to works also with drush aliases?
If I write "drush sql-sync @a" and then click on "tab", I would like to have autocompletation for aliases that start with "a".