Drush advanced, setup with composer, aliases and autocomplete

In a previous article I wrote a small review of Drush and useful commands, this article is going a little farther configuring Drush on your *nix system.

Manual Drush installation

There is probably a drush package on your system, but if you want to use the last version, you need to make the (semi) manual installation.
Drush has migrate from Pear to Composer system, and now there is a phar script... official documentation is a bit light on this subject, for a complete installation under *nix (I am using a distribution which uses Yum), you must first install Composer, preferably in your user directory


Setup Composer globally on your system:

curl -sS | php
mv composer.phar /usr/local/bin/composer

Add composer bin path to your PATH

sed -i '1i export PATH="$HOME/.composer/vendor/bin:$PATH"' $HOME/.bashrc
source $HOME/.bashrc

Installation of Drush, you will need git on your system:

yum install git
composer global require drush/drush:dev-master

Update will be handle with

composer global update

Init drush

drush init

If everything goes fine you now can use Drush on your system, if you get an error, you probably need to source your .bashrc!

Add some useful aliases to your system

The drush init adds basic aliases to your system, but you can add more useful ones:

# Watchdog log
alias dws='drush watchdog-show --tail --count=5'
# Delete watchdog log
alias wda='drush wd-del all -y'
# Flush all images styles
alias ifa='drush image-flush --all -y'
# Features aliases
alias dfd='drush features-diff'
alias dfr='drush features-revert'
alias dfr='drush features-revert all'
alias dfu='drush features-update'
# Search in Drush commands
alias drs='drush | egrep'

You now have a lot of aliases for Drush on your system, the most useful will be

cca = 'drush cache-clear all'
dl = 'drush pm-download'
st = 'drush core-status'
dis = 'drush pm-disable'
en = 'drush pm-enable'

Add completion for Drush commands

Very useful completion works with TAB keyboard, every unix users probably use it.

First you must be sure bash completion is on your system:

yum install bash-completion

You can make a symbolic link

ln -s $HOME/.composer/vendor/drush/drush/ /etc/bash_completion.d/

And restart your bash

exec bash

To test it simply start a drush command:

drush pm

Press TAB to get all the available commands (first TAB can be slow...) and you will get:

drush pm
pm-disable pm-enable pm-info pm-list pm-releasenotes pmu pm-update pm-updatecode-postupdate pm-download pmi

Drush aliases on multisite or multi servers

A last useful point with Drush will be creation of site aliases, if you created a site by using the directory management of Drupal (/sites/my_website instead of /sites/default) or multisite feature of Drupal, this functionality will make you gain some time.
You need the php-posix extension on your system (php-process under centos).

You can create one alias with Drush if you are already in a website root

drush site-set @test

Then you can throw Drush commands anywhere for this site

drush @test status

But this alias will work only on the current session, to get a permanent alias, you must copy the drush file in your environment

cp $HOME/.composer/vendor/drush/drush/examples/example.aliases.drushrc.php $HOME/.drush/aliases.drushrc.php

You can then adapt aliases at the end of the file, it will allow you to execute a drush command for a site, even if you are not on the root folder, example from the aliases.drushrc.php file

$aliases['test'] = array(
  'uri' => '',
  'root' => '/chemin/vers/racine/drupal',

From anywhere you can execute

drush @test status