fromJune 2015
Article:

Drush: The Swiss Army Knife for Drupal

Part Five: Shell Aliases and Smart Defaults Make a Happy Developer
0

Photo of myriad things By now, if you have been using Drush for a while I assume you are comfortable with all the basics. (I consider the basics to be things like downloading modules or updating a site.) I also assume you are aware that Drush has plenty of other features built in, but you probably don't take advantage of them. In this article I want to show you a few things that are just as easy to use as the “basics” and only require a little upfront setup to use them. Once you learn them, they will quickly find their way into your daily workflow.

Shell Aliases

Consider how much time you spend typing out commands. Now think of all the commands you type over and over again. Next, think about all the commands that have a lot of options and how often you refer to help resources to remember which options you need to use. Wouldn’t it be better if we simplified those things? Conveniently, Drush allows you to do exactly that: create shortcuts or aliases in a file called drushrc.php. I'll refer to it as the command file later in this article.

Let's start with an easy example: the clear cache command. This command isn't very long but we can still improve on it. Plus, it's probably one of the most frequently used.

Edit your drushrc.php file; if it doesn't exist create it. It's typically in your home folder at:

~/.drush/drushrc.php

Add this line to the bottom of this file and save it:

$options['shell-aliases']['ca'] = 'cache-clear all';

We just added a shell alias. Now instead of typing this:

$ drush clear-cache all

You only need to type:

$ drush ca

That wasn't too hard, right? That was one line of code, and you just improved on an already very simple command.

Let's take a closer look at the line we added to the command file. The first part $options['shell-aliases'] tells Drush we are adding an alias. The second part, ['ca'] =, declares what the name of the alias should be. Last, we tell it what our named alias should do: 'cache-clear all';. Replicate this and replace the name and the command to create your own.

What if I told you that shell commands can have a Drush alias too?

Well, they can and it's just as easy as the clear-cache example. They are structured the same way, with one minor difference: you need to prefix the command with an exclamation mark. I'll use git clone as an example. Let's say you want to create a shortcut for downloading the latest Drupal 8 from git. I will name my alias “gcd” for Git Clone Drupal.

Edit the same drushrc.php file and add the line below:

$options['shell-aliases']['gcd'] = '!git clone --branch 8.0.x http://git.drupal.org/project/drupal.git';

Now let’s give our alias a try by issuing this command from an empty folder:

drush gcd

Drush will execute that git clone command on your behalf and download the project into a folder named “drupal”. Seeing how easy it is to add aliases, I hope you are now thinking about all the possible aliases that can improve your development.

Smart Defaults

The command file also allows us to provide defaults for commands. This is particularly helpful for the commands that get your fingers in a tangle when you type them out. A good example of this might be the SQL dump command. Here's what I mean:

drush sql-dump --result-file=~/backups/myfile.sql –structure-tables-list=cache,watchdog

The above command will dump the database to a specific file but not include the data from the cache or watchdog tables. It can also be a lot of unnecessary typing since we can set these options as defaults. Edit the same drushrc.php from before and insert the following lines:

$options['result-file'] = '~/backups/@DATABASE_@DATE.sql';

That line will now tell Drush to always save the dump file in a folder named backups. With the use of the @DATABASE and @DATE tokens the filename will also contain the database name and the date of the dump.

But what about the second part of the command? From the above example, I explicitly asked Drush to ignore the data from two tables. Often, it’s unnecessary to export the data from tables like the cache. Since I often don't want data from certain tables, I will create a list of common tables to ignore. Using wildcards, I'll expand the list and ignore any cache table and a few other things.

Add this line to the drushrc.php file:

$options['structure-tables']['common'] = array('cache', 'cache_*', 'history', 'search_*', 'sessions', 'watchdog');

Finally, we're going to add a line that will combine the smart defaults into an alias. Insert this line into your drushrc.php file:

$options['shell-aliases']['dump'] = 'sql-dump –skip-tables-key=common';

With three lines of code we have converted that long sql-dump command into two words:

$ drush dump

Think of all the commands you can create to make your daily development easier.
The bonus for actually taking the time to create your own custom commands is that you will only need to do it once. Keep a copy of your commands handy in a text file somewhere. Then whenever you enter a new Drush-enabled environment, paste your pre-made commands into the new drushrc.php file. You'll be up and running with all your familiar commands right away.

Image:"Kit para emergências do dia-a-dia" by Dois Espressos is licensed under
CC BY-NC 2.0