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<br>$ 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<br>$ drush pm-disable faceted_search
can thus be shortened to simply
$ drush en faceted_search<br>$ 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.<br># To be added at the end of .bashrc.<br>alias drsp='cp sites/default/default.settings.php sites/default/settings.php'<br>alias drcc='drush cache-clear all'<br>alias drdb='drush updb && drush cc all'<br>alias drdu='drush sql-dump --ordered-dump --result-file=dump.sql'<br>alias dren='drush pm-enable'<br>alias drdis='drush pm-disable'<br>alias drf='drush features'<br>alias drfd='drush features-diff'<br>alias drfu='drush -y features-update'<br>alias drfr='drush -y features-revert'<br>alias drfra='drush -y features-revert all'<br>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()<br>{<br> COMPREPLY=( $( compgen -W '$( command find $1 -regex ".*\.module" -exec basename {} .module \; 2> /dev/null )' -- $cur ) )<br>}<br><br>_drupal_modules()<br>{<br> local cur<br> COMPREPLY=()<br> cur=${COMP_WORDS[COMP_CWORD]}<br> # We temporarily assume we are in the Drupal root.<br> _drupal_modules_in_dir "sites profiles modules"<br>}
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() {<br> # Go up until we find index.php<br> current_dir=`pwd`;<br> while [ ${current_dir} != "/" -a -d "${current_dir}" -a \<br> ! -f "${current_dir}/index.php" ] ; <br> do<br> current_dir=$(dirname "${current_dir}") ;<br> done<br> if [ "$current_dir" == "/" ] ; then<br> exit 1 ;<br> else<br> echo "$current_dir" ;<br> fi <br>}
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<br>$ cdd faceted_search
Autocomplete a module name (example: "fac" for "faceted_search") and enable it:
$ dren fac # Then press TAB<br>$ dren faceted_search
Autocomplete a feature name and revert it:
$ drfr n # Then press TAB<br>$ drfr nuvole_<br>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.
Tags: Drupal Planet, DrushAttachments: drush-bash-tricks.txtImage: