Debug drush requests in PHPStrom

Today I needed to debug Drush request. Since I've been using PHPStorm for last few weeks I wanted to use it for that. After a bit of research I found this way to achieve what I needed:

  1. Install and configure Xdebug. There are plenty posts about this all over the internet.
  2. Start normal web debug session in PHPStorm. Look for "Waiting for incoming connection with ide key '17862'." in PHPStorm's debug window. 17862 will be something else in your case. Remember this number as we're going to need it in next steps.
  3. Open terminal and set new variable
    export XDEBUG_CONFIG="idekey=17862"

    Remember to replace "17862" with the number you remembered in the previous step.

  4. Run your drush command and PHPStorm should stop on first breakpoint that you've set.

Tadaaaa! :) That was easy. I'm not sure if there is any even more convenient way to do this. If you're aware about it, please feel free to leave a comment.