I hit ⌘D⌘R, you won't believe what happened next!

Submitted by Frederic Marand on
OK, so you know the contents has almost nothing to do with the pseudo-clickbait-y title, right ? Well, actually it has: this is about the single most useful command in Drupal development. Guess which ?

Keep calm and flush cache

If you've done any Drupal development in the last ten years or so, you have probably noticed that the single most often used command in your work process is clearing the cache, be it in the Drupal UI at admin/config/development/performance (or its earlier incarnations), or hopefully with drush cr in Drupal 8 or drush cc all in earlier versions.

The thing is, while you probably switched from the web UI to Drush years ago, it's still a minor annoyance to have switch to a terminal, press the ▲ / ↵ combo and switch back to PhpStorm. Or switch view from the editor to the Terminal view. Isn't there a better way ?

You bet there is: as soon as I asked myself I knew it had to be easy, so let's see how it's done in PhpStorm in three steps:

  1. create an external tool
  2. map a key to it
  3. trick: there's no third step, it's already done

drush cr in PhpStorm 2016.3

Here is in detailed form how to implement the two steps above. Note that these instructions assume that (a) you are working in a Drupal 8 project opened one level above the Drupal docroot, as is typical in Composer-built projects, (b) Drush is local to the project, as it probably should. Adjust according to your actual situation.

Choice Value Notes
Open tool dialog
  • macOS: PhpStorm / Preferences / Tools / External Tools / "+"
  • Linux: File / Settings / Tools / External Tools / "+"
Name Drush CR Or whatever...
Description Keep calm and flush cache! ...fits your fancy
Group External tools Although the widget looks like a selector, you needn't stick to existing groups, but may create one just by typing its label.
Synchronize files after execution Checked If you haven't excluded sites/default/files you may be interested in the changes wrought by the command. Otherwise, keep unchecked to reduce IDE work.
Open console Checked You normally want to see if something happens during the flush. Don't worry: only one console gets used for all calls to the tool.
Show in As you prefer Probably at least in editor views
Program $ProjectFileDir$/vendor/bin/drush Assuming you are working on a Composer-built project
Parameters cr Or cc all for D7 projects. Yes, you can use Composer for these too.
Working directory $ProjectFileDir$ Especially important for projects built on drupal-composer/drupal-project where Drupal is not the project root: you want to run Drush from the Drupal root.
Open keymap dialog
  • macOS: PhpStorm / Preferences / Keymap
  • Linux: File / Settings / Keymap
Search for your tool Drush CR Or whatever you typed in the Name field above
Map a key Your choice: I used ⌘D⌘R Now you know what happened when I hit ⌘D⌘R