#!/bin/bash # ABOUT : BASH Shell auto completion for Drush - it auto-completes when you invoke drush with - 'd', 'dr', 'drush' or 'drush.php'. # WEB RESOURSES : See http://drupal.org/node/437568 - the issue that we use for this thing. # VERSION : 0.9 (entirely bash based, no php helper file or dedicated backend) # CHANGELOG : # : 0.9 - made it Mac OSX compatible I hope; renamed some vars # : 0.8 - changed the way drupal_root is determined # : 0.7 - we handle 'sql dump'-like commands correctly # DATE : 090528 # INSTALL : (valid for debian with bash) # : EITHER 1. cd /etc/bash_completion.d/; ln -s /path/to/drush/drush.completion # : OR 2. mcedit .bashrc; then write ". /path/to/drush/drush.completion" in it ('.' = source) # REQUIREMENTS : "drush" named symlink in your $PATH . It uses it to get some info by calling drush. # TODO : Limitations : # : 1. better modules/projects completion on dl/enable/disable ... for now we do a `find $drupal_root/modules/ $drupal_root/themes/ $drupal_root/sites/ -type f -name "*.info" -exec basename '{}' .info \; | tr '\n' ' '` ... which returns ALL present modules/themes # TODO : Roadmap : # : create an internal use --completion option for drush that will make smarter completion suggestions # The func that bash uses for drush completion. _drush_completion() { local cur prev # init the array with the reply COMPREPLY=() # part of currently completed word cur=${COMP_WORDS[COMP_CWORD]} # prev completed word prev=${COMP_WORDS[COMP_CWORD-1]} drc_drupal_root=`drush help -v -b | tr '"' '\n' | grep 'root directory at' | grep -E -o '\\\\.*' | tr -d '\'` # results to sth like "/var/www/vhosts/example.com" - no new line at its end # this is quite faster than `drush status --pipe` or `drush status --backend` # find all things with .info files below drc_all_modules=`find $drc_drupal_root/modules/ $drc_drupal_root/themes/ $drc_drupal_root/sites/ -type f -name "*.info" -exec basename '{}' .info \; | tr '\n' ':'` # output drush commands or .info files (projects and their modules) case $prev in enable) drc_word_list=$drc_all_modules ;; disable) drc_word_list=$drc_all_modules ;; uninstall) drc_word_list=$drc_all_modules ;; update) drc_word_list=$drc_all_modules ;; updatecode) drc_word_list=$drc_all_modules ;; info) drc_word_list=$drc_all_modules ;; # these above are the module commands *) # list all drush commands drc_word_list=`drush help --pipe | sed s/\"\ \"/\:/g | sed s/\"/\:/g` # makes them :-delimited ;; esac if echo $drc_word_list | grep "\:$prev">/dev/null then # echo "dA:$prev:::I$drc_word_list" compgen_input_word="$prev $cur" oldIFS=$IFS # we need to hack IFS because completion of space-containing drush commands is not a nice task the least ;) IFS=: # generate completion suggestions based on already typed string completions=`compgen -W '$drc_word_list' -- $compgen_input_word | tr "\n" ":" | sed s/$prev\ //g` # echo "<$completions>" # gives them one on per line else # echo "NE:$prev:::I$drc_word_list" compgen_input_word="$cur" oldIFS=$IFS # we need to hack IFS because completion of space-containing drush commands is not a nice task the least ;) IFS=: # generate completion suggestions based on already typed string completions=`compgen -W '$drc_word_list' -- $compgen_input_word | tr "\n" ":"` fi COMPREPLY=( $completions ) IFS=$oldIFS } complete -F _drush_completion d dr drush drush.php