Web Developer Workflow: More Useful Linux Bash Aliases

More Bash Alias fun. This time we'll look at Git, Drush and a few others.

Intro

This is a followup to another recent post discussing bash aliases meant to make your web-developer life easier. If this is your first Tango with Bash Aliases, head to that post for an intro to working with aliases. Otherwise--the list goes on!

Let's start with some absolute basics on bash. A simplistic definition of bash is basically to think of it as your terminal--or, rather, the application that handles all those commands you type into the terminal. There are other applications (shells) that can do this, but bash is perhaps the most common (you can also find it on OSX). If you're on the command-line often, you'll know that certain tasks and commands can require a mouthful of keys and 'options'. These can be sometimes difficult to remember; they can also require pounding on your keyboard a bit more than you'd really like. This is where bash aliases come in handy! Think of them as shortcuts; a way to take those long-winded multi-key commands and convert them into a single word--an alias.

As a web-developer, these are some of the more useful aliases I've leveraged for making my workflow just a wee bit more efficient. Here's a quick jump menu if you want to skip to a certain alias:

Git: Checkout Last Branch

I often have to entertain multiple stakeholders, which means I'm often merging several branches into a "preview branch" which I dedicate to a specific environment (i.e., DEV). When you're managing branches like this, it can mean for quite a bit of "branch hopping": checking out branch b, committing, checking out branch a, merging, checking out branch b, again. This can take it's toll on your fingers. Here's something cool you may not have known, though: Git has a native alias that will allow you to checkout whichever branch you were last on. There are two ways to do it actually:

//FROM THE COMMAND LINE
$ git checkout - 

//WHICH IS ACTUALLY SHORTHAND FOR:
$ git checkout @{-1}

I've been using Git for about 6 years and I only just now caught onto this. You can imagine the smile that slapped my face when I recently learned this. That's a "game changer" if ever there was one. It also opens an entire world of Bash Alias fun. First of all, it means we can now check out the last branch we were on with a single word--like "last":

#IN YOUR .bash_aliases FILE, OF COURSE
alias last='git checkout -' 

You can actually setup aliases like this in your .gitconfig file (should be in your $HOME directory), but since we're focused on Bash, we'll leave that for another day. Let's see what else we can do...

Git: Checkout Master & Merge Last Branch

As I've already stated, when you're tying a bunch of branches into a "preview branch" you wind up doing a lot of merging. We can use that same "last branch" shorthand with our merges, though. So, here's another one that might save you a few keystrokes: "checkout <preview branch> and merge the last branch into it". This is particularly useful if you're working on a branch and need to tie it into the preview branch in a jiff. I use a dedicated preview branch called master-dev. So, here's what the alias would look like. You can, of course, also use this for merging into master.

# GIT MERGE LAST BRANCH INTO PREVIEW BRANCH
alias merge@mdev='git checkout master-dev && git merge -'
# IF FOR WHATEVER REASON YOU WANTED TO USE THE NON-SHORTHAND VERSION BE SURE TO ESCAPE
alias merge@mdev='git checkout master-dev && git merge \@\{-1\}'

# AGAIN, SWAP BRANCHES WITH WHATEVER IS APPROPRIATE TO YOUR WORKFLOW -- i.e., MASTER

Drush: Database Imports

This one assumes you have Site Aliases setup for Drush. For me, this one is such a biggie it was actually worth the pain to get the aliases setup. Luckily, for the site I work on most, our host provides downloadable Site Aliases for all our remote sites. Otherwise, doing it yourself is a bit of a pain, but not too hard. With the aliases intact you can tell Drush to bring down a copy of one of your remote databases and load it into your local. Without the Drush Site Aliases, this is something you'd have to do with PHPMyAdmin: go download a DB Backup from remote; drop all the tables in your local DB, and then import. I've done it enough times with PHPMyAdmin to know that the Drush aliases are worth it. Here's what the Drush 10 syntax would normally look like--although it can be made a lot simpler with a Bash Alias:

//FIRST, WE'D HAVE TO DROP ALL TABLES ON THE LOCAL DB
$ drush sql:drop
//ADD THIS LINE
$ drush sql:sync <SOURCE SITE ALIAS> <TARGET SITE ALIAS>

//SITE ALIASES OF COURSE WOULD LOOK THIS
@sitename.env
i.e., @terracoders.prod 

We could alias both of these commands in Bash so that we only need to type one thing. If working with multiple remote environments, you could even name them accordingly:

# BRING IN A DB FROM DEV
alias db@dev='drush sql:drop && drush sql:sync @sitename.dev @sitename.local'
# BRING IN A DB FROM DEV
alias db@stage='drush sql:drop && drush sql:sync @sitename.stage @sitename.local'
# BRING IN A DB FROM DEV 
alias db@prod='drush sql:drop && drush sql:sync @sitename.prod @sitename.local'

This could actually be made into a shell script--where we prompt the user for the source alias (or source and target both) and then execute the commands. I'll probably put together a post on this in the near future.

Open Terminal Directory w/ Thunar

Here's an easy one. I use Thunar as my file manager (running Xubuntu). If you're deep in the file-system on your terminal and you'd like Thunar to open a window for whatever directory you're in, all you need to do is type thunar. That alone is super cool, but I want a duplicate alias that's a little more intuitive:

# OPEN CURRENT DIRECTORY IN THUNAR FILE MANAGER WINDOW
alias open='thunar'

I haven't tested this on Ubuntu with Nautilus, but it looks like you could do the same:

alias open='nautilus .'

Find Aliases by Keyword

Here's another simple one, but kind of meta and definitely useful. The more aliases you create, the more you have to remember. If you're someone who's only beginning to create your own aliases, you might forget the actual aliases you've created. You can type alias into your terminal and get a rather unorganized list of all the aliases on your system, but this isn't particularly useful if you're trying to remember a specific alias. You could, of course, pipe the alias command with grep and do a search to narrow down the output; it's much more fun to do this with an alias though:

//fa IS MY SHORTHAND FOR "find alias"
alias fa='alias | grep' 

//USAGE EXAMPLE
$ fa git

Here's an example of the output:

finding aliases by keyword with grep
Git with the program!

Conclusion

I'll leave it at that for now. Probably more to come. Hopefully you find at least some of these useful. Even if they don't work for your specific workflow, I hope they inspire you to do some "aliasing" of your own.