Last Updated: February 25, 2016
·
491
· ldavisrobeson

Drush shortcuts & autocomplete (for Features and modules)

Put this in your .bash_profile and smoke it (actually you just save it and reload with source ~/.bash_profile):

#########################
# Drush shortcuts & autocomplete (for Features and modules)
# Aliases for aliases!
#
# All commands & aliases: drushcommands.com
#########################
alias d="drush" # Save 4 keystrokes, yo!
alias dcca="drush cc all" # Clear all Drupal caches
alias den='drush pm-enable'
alias ddis='drush pm-disable'
alias df='drush features'
alias dfd='drush features-diff'
alias dfl="drush features-list" # List all the available features for your site
alias dfr='drush -y features-revert'
alias dfra='drush -y features-revert all'
alias dfu='drush -y features-update'
alias dst="drush status" # Birds-eye view of current Drupal installation, if any 

alias create-admin-acct="drush user-create yourusername --mail=yourusername@domain.com && drush user-add-role administrator --name=yourusername && drush uli --name=yourusername" # Create admin acct (helpful after syncing with a db you don't have access to)

#########################
# The following is slightly modified from:
# Drush-Bash tricks 0.1 (http://nuvole.org/node/26)
# Copyright Nuvole 2010
# License: GPL 3, see http://www.gnu.org/licenses/gpl.html
#########################

_drupal_root() {
  # Go up until we find index.php
  current_dir=`pwd`;
  while [ ${current_dir} != "/" -a -d "${current_dir}" -a \
          ! -f "${current_dir}/index.php" ] ; 
  do
    current_dir=$(dirname "${current_dir}") ;
  done
  if [ "$current_dir" == "/" ] ; then
    exit 1 ;
  else
    echo "$current_dir" ;
  fi 
}

_drupal_modules_in_dir()
{
  COMPREPLY=( $( compgen -W '$( command find $1 -regex ".*\.module" -exec basename {} .module \; 2> /dev/null )' -- $cur  ) )
}

_drupal_modules()
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  local drupal_root=`_drupal_root` && \
  _drupal_modules_in_dir "$drupal_root/sites $drupal_root/profiles $drupal_root/modules"
}

_drupal_features_in_dir()
{
  COMPREPLY=( $( compgen -W '$( command find $1 -regex ".*\.features.inc" -exec basename {} .features.inc \; 2> /dev/null )' -- $cur  ) )
}

_drupal_features()
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  local drupal_root=`_drupal_root` && \
  _drupal_features_in_dir "$drupal_root/sites $drupal_root/profiles $drupal_root/modules"
}

# Example usage, revert a feature for project namespaced "foo":
# dfr foo_  # Then press TAB to trigger autocomplete (takes a second to load, be patient)
complete -F _drupal_modules den
complete -F _drupal_modules ddis
complete -F _drupal_features dfd
complete -F _drupal_features dfr
complete -F _drupal_features dfu