Typing less in Drush: aliases and autocompletion

Andrea Pescetti
9 Nov 2010
7 Comments
Andrea Pescetti
9 Nov 2010
7 Comments
Andrea Pescetti, 9 Nov 2010 - 7 Comments

Typing less in Drush: aliases and autocompletion

How to work more efficiently with some Drush-Bash tricks

While Drush is a big time saver, the textbook version of some commands can become lenghty at times:

$ drush pm-enable faceted_search
$ drush features-revert nuvole_news

An out-of-the-box Bash completion for Drush is in the works, and the Drush shell ("drush core-cli") already offers fast access to commands and fancy features. However, if you prefer not to leave your shell and use a stock Drush, here are some tips for a faster Drush experience involving aliases and autocompletion.

Drush and Bash Aliases

In this section we'll assume you use the Bash shell, which may not be the default on your system; however, the techniques listed here can work, or be adapted to work, on other shells too.

Drush aliases

Most Drush commands provide aliases, so it's relatively rare that you have to type the full command. Just look them up in the Drush help page.

For example, the short form of pm-enable is just en, while the short form of pm-disable is just dis. Typing

$ drush pm-enable faceted_search
$ drush pm-disable faceted_search

can thus be shortened to simply

$ drush en faceted_search
$ drush dis faceted_search

Bash aliases for Drush

You can configure Bash in order to define shortcuts for the most frequently used commands, including Drush constructs. This is done using the alias builtin. Normally you'll want to add the list of aliases to the .bashrc file in your home directory (or/and to .bash_profile or .profile, depending on your configuration; you might have to create it from scratch too).

Here is a sample configuration for a workflow based on Features and Drush; of course, adapt to taste.

# Drupal and Drush aliases.
# To be added at the end of .bashrc.
alias drsp='cp sites/default/default.settings.php sites/default/settings.php'
alias drcc='drush cache-clear all'
alias drdb='drush updb && drush cc all'
alias drdu='drush sql-dump --ordered-dump --result-file=dump.sql'
alias dren='drush pm-enable'
alias drdis='drush pm-disable'
alias drf='drush features'
alias drfd='drush features-diff'
alias drfu='drush -y features-update'
alias drfr='drush -y features-revert'
alias drfra='drush -y features-revert all'
alias dr='drush'

To be able to use the new aliases, you need to run source .bashrc, or just logout and login again. Note that by typing dr and then the TAB key twice you will see all aliases.

Using autocompletion

The advanced Bash Completion is a tremendously efficient and addictive extra functionality for Bash: it extends the traditional Bash TAB key completion in a smart way, so that by typing ssh and then pressing TAB you get a list of known hosts, and similarly gunzip and then TAB will show you the list of .gz files only. We want to use this powerful functionality to autocomplete modules (or themes, easily extendable) and features names in Drush.

We will first need a function that generates all project names. This could be done in a semantic way with drush pm-list --pipe, but this would involve bootstrapping Drupal and it can at times be too slow when typing. We thus choose a syntactic approach and complete based on results of the find utility, which is less smart since it cannot query the Drupal database and act accordingly, but is generally much faster.

We assume we are in the Drupal root for the time being; then we will remove this limitation. A textbook completion function is:

_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]}
  # We temporarily assume we are in the Drupal root.
  _drupal_modules_in_dir "sites profiles modules"
}

To specify that this completion function is to be used to autocomplete the argument of, say, dren, we will just have to specify (a full example will follow):

complete -F _drupal_modules dren

Now let's see how to find the Drupal root. As a byproduct we will be able to type cdd views and go from anywhere in the Drupal root to the directory containing the Views module. Again, the smart way would involve a (slow) call to Drupal, so we use filesystem-based heuristics.

_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
}

These snippets can now be assembled to obtain a full solution, ready to be added to your .bashrc file and able to handle Features and all aliases.

The full package of Drush-Bash tricks to add to your .bashrc file can be found in attachment.

But remember that everything should be adapted to your needs and configurations: for example, you might want to handle themes too, or use conventions to place features in a features/ subdirectory and thus improve performance, or add support for site aliases...

For a system-wide and cleaner implementation, you can copy the second part of the Drush-Bash tricks file to a file named /etc/bash_completion.d/drush_custom.

Examples

Time to play with our new functions! Here are a few use cases.

Change to the Drupal root from anywhere within the Drupal directory tree:

$ cdd

Autocomplete a module name (example: "fac" for "faceted_search") and change into its directory:

$ cdd fac    # Then press TAB
$ cdd faceted_search

Autocomplete a module name (example: "fac" for "faceted_search") and enable it:

$ dren fac    # Then press TAB
$ dren faceted_search

Autocomplete a feature name and revert it:

$ drfr n    # Then press TAB
$ drfr nuvole_
nuvole_cases      nuvole_site       nuvole_solutions

From anywhere within the Drupal tree, go back to the Drupal root, run svn update and come back again to the previous working directory:

$ (cdd && svn up)

The techniques explained in this article are part of the Nuvole Code-Driven Drupal Development course and will be covered in deeper detail in the DrupalCon Chicago 2011 training by Nuvole: Code-driven Development: Use Features Effectively.

drush-bash-tricks.txt (2.43 KB)Download

Comments

Comments

yareckon
9 Nov 2010

Thanks for writing this... already using drush and bash aliases, but learned a little something about writing a bash completion function! Sweet.

Thanks for writing this... already using drush and bash aliases, but learned a little something about writing a bash completion function! Sweet.

yareckon, 9 Nov 2010

Thanks for writing this... already using drush and bash aliases, but learned a little something about writing a bash completion function! Sweet.

yareckon, 9 Nov 2010
dixon_
9 Nov 2010

Wow, I'll go ahead and try these tricks right away! I believe they can save a lot of time for us spending a lot of time with Drush :) Thanks for sharing!

Wow, I'll go ahead and try these tricks right away! I believe they can save a lot of time for us spending a lot of time with Drush :) Thanks for sharing!

dixon_, 9 Nov 2010

Wow, I'll go ahead and try these tricks right away! I believe they can save a lot of time for us spending a lot of time with Drush :) Thanks for sharing!

dixon_, 9 Nov 2010
This is so bad ass!
10 Nov 2010

Thank you so much for writing this article. I should write you a check for all the time this has saved me this morning alone.

Thank you so much for writing this article. I should write you a check for all the time this has saved me this morning alone.

This is so bad ass!, 10 Nov 2010

Thank you so much for writing this article. I should write you a check for all the time this has saved me this morning alone.

This is so bad ass!, 10 Nov 2010
Andrew Burcin
14 Nov 2010

Very useful post. I've had about the same bash aliases set for a while, but the auto-complete definitely is an improvement.

'cdd' is really useful, I'm wondering if it could also be applied to allow auto-complete of theme directories too...

For my personal use, I've made a git repo with the file of your drush aliases and autocomplete, and a file with my git aliases. Instead of pasting this content into bashrc, you can simply checkout the repo, at the end of .bashrc load if file exists, making maintaining and updating much easier across all the machines you use.

Very useful post. I've had about the same bash aliases set for a while, but the auto-complete definitely is an improvement.

'cdd' is really useful, I'm wondering if it could also be applied to allow auto-complete of theme directories too...

For my personal use, I've made a git repo with the file of your drush aliases and autocomplete, and a file with my git aliases. Instead of pasting this content into bashrc, you can simply checkout the repo, at the end of .bashrc load if file exists, making maintaining and updating much easier across all the machines you use.

Andrew Burcin, 14 Nov 2010

Very useful post. I've had about the same bash aliases set for a while, but the auto-complete definitely is an improvement.

'cdd' is really useful, I'm wondering if it could also be applied to allow auto-complete of theme directories too...

For my personal use, I've made a git repo with the file of your drush aliases and autocomplete, and a file with my git aliases. Instead of pasting this content into bashrc, you can simply checkout the repo, at the end of .bashrc load if file exists, making maintaining and updating much easier across all the machines you use.

Andrew Burcin, 14 Nov 2010
Andrew Burcin
14 Nov 2010

By changing each occurrence of '.module to '.info', the completion scripts will find themes.

By changing each occurrence of '.module to '.info', the completion scripts will find themes.

Andrew Burcin, 14 Nov 2010

By changing each occurrence of '.module to '.info', the completion scripts will find themes.

Andrew Burcin, 14 Nov 2010
Andrea Pescetti
14 Nov 2010

Yes, but then you would catch both modules and themes... In the end, it's a trade off and as I wrote everybody should customize scripts to his needs: I find it useful to autocomplete modules only, because I never need to enable/disable themes via Drush, others with different needs or different workflows may want to tweak scripts to their liking.

Yes, but then you would catch both modules and themes... In the end, it's a trade off and as I wrote everybody should customize scripts to his needs: I find it useful to autocomplete modules only, because I never need to enable/disable themes via Drush, others with different needs or different workflows may want to tweak scripts to their liking.

Andrea Pescetti, 14 Nov 2010

Yes, but then you would catch both modules and themes... In the end, it's a trade off and as I wrote everybody should customize scripts to his needs: I find it useful to autocomplete modules only, because I never need to enable/disable themes via Drush, others with different needs or different workflows may want to tweak scripts to their liking.

Andrea Pescetti, 14 Nov 2010
subhojit777
14 Aug 2013

Thanks for sharing. Really very helpful and time saver.

Thanks for sharing. Really very helpful and time saver.

subhojit777, 14 Aug 2013

Thanks for sharing. Really very helpful and time saver.

subhojit777, 14 Aug 2013

Get your project started today!

Contact us