Web Developer Workflow: Useful Linux Bash Aliases

If you spend a lot of time on the command line, like I do, an important part of your workflow is getting things done both quickly and efficiently. Bash aliases can be extremely helpful in saving you time. This isn't a comprehensive list by any means, but hopefully it serves as a starting point--allowing you to optimize your terminal-based development work.

Intro

I recently talked about bash aliases for managing XAMPP more efficiently. Here are a few more fun ways you can optimize your development workflow on the command-line using aliases. None of this is rocket science, by any measure. Still, if you're new to bash aliases this brief article will hopefully act as an introduction to a new way of using the command-line (and a new way of thinking about workflow, hopefully).

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:

Creating Aliases

In order to create aliases, all I need to do is edit the .bash_aliases file in my user folder (i.e., /home/username). Now, you may not have a .bash_aliases file in your user folder if you've never created an alias before, but that shouldn't really matter. You should have a .bashrc file in your user directory (ctl+h to reveal in your file browser, or ls -a to reveal on the command-line), and this file should already reference the .bash_aliases file (as an include, of sorts) regardless of whether the file exists. If you don't have a .bash_aliases file yet, the directions below shouldn't really change. An important note on editing your .bash_aliases: each time you add an alias or make a change, you'll need to reload your .bashrc file. This is covered in the directions below. Don't forget to do it, though, or you may not initially see your aliases work.

Navigating Directories w/ cd

I keep the files for multiple sites a couple of directories down from my user folder (i.e.; ~/Sites/site-name/docroot). On some days, I'm in and out of those directories (one in particular, actually) from the command line; typing out a long path each time I need to get back to my site's root folder can be a real pain. There are ways of coping with this (see my side note below), but it would nonetheless be helpful if I could find a way of getting back to /docroot with something shorter than cd ~/Sites/site-name/docroot. Wouldn't it be cool if I could just type the word 'site' into my terminal and find my way to /docroot? It's quite easy to setup, actually! I'll head into my user directory and edit my .bash_aliases file like this:

//IF NOT ALREADY IN USER DIRECTORY, CD WITHOUT ANY PATH TO GET THERE
$ cd 

//EDIT OR CREATE THE .BASH_ALIASES FILE WITH NANO
$ sudo nano .bash_aliases
//ADD THIS LINE
alias site='cd /path/to/your/docroot'

ctl+o to save, ctl+x to exit

//**************************************//
//IMPORTANT BIT: RELOAD THE BASHRC FILE //
//**************************************//
$ source .bashrc

If I hit the command line and simply type 'site' now, I'll find my way to my site's document root; obviously, if you had multiple sites to work on, you could setup unique aliases to find your way to each respective document root. Cool!

On a side note: Another useful command is cd -. This is a shorthand command that will get you back into whatever directory you were last in.

CD and Git Status

If you're like me, one of the first things you do when you get to a docroot folder is to run git status. If that's the case, we could include git status in the bash alias we created above to save us a few extra keystrokes. Without changing the alias, I'll just tag on an extra command in the alias definition. Once again, from your user directory:

$ sudo nano .bash_aliases
//ADD THIS LINE
alias site='cd /path/to/your/docroot && git status'

ctl+o to save, ctl+x to exit

//RELOAD BASHRC FILE
$ source .bashrc

Now, if I enter 'site' on the command line, I'll find my way to my document root and get the results of git status at the same time.

SSH & Long Server Names

If you find yourself SSH'ing into servers with any frequency, and, as with some webhosts, those server domains are unnecessarily long, you might save yourself a good number of key-strokes with a bash alias. Likewise, if you don't SSH into a server very often (but nonetheless need to once every blue moon), an alias might keep you from having to go figure out what that long domain was once you've forgotten it. In either case, creating an alias for an SSH login is quick and easy--here, I've chosen 'sitessh' as my alias:

$ sudo nano .bash_aliases
//ADD THIS LINE
alias sitessh='ssh username@srv-1234.whatever-that.long.domain-was.com'

ctl+o to save, ctl+x to exit

//RELOAD BASHRC FILE
$ source .bashrc

GIT

In my workflow, I often find myself doing repetitive tasks on Git: checking out, pulling, adding, committing, etc. It's not a good idea to combine or abbreviate these tasks unless you know what you're doing; even without aliases, it's easy to get turned around on Git. Still, if you're somewhat comfortable with Git-based workflow, you might try combining a few of the more commonly paired commands. One such pair might be checking out your master branch locally followed by a pull from remote. If you're confident that master doesn't have any unstaged local changes, you could probably get away with combining these (not that it would really be the end of the world otherwise). Using the alias 'master', that would look like this:

$ sudo nano .bash_aliases
//ADD THIS LINE
alias master='git checkout master && git pull origin master'

ctl+o to save, ctl+x to exit

//RELOAD BASHRC FILE
$ source .bashrc

Another common pair of commands are Add and Commit. Again, an erroneous commit can lead to more work on the command-line, so probably best to combine these only when you're sure that your changes are rock-solid. Otherwise, you can combine them like this--I've chosen the alias 'addcommit':

$ sudo nano .bash_aliases
//ADD THIS LINE
alias addcommit='git add -A && git commit -a -m'

ctl+o to save, ctl+x to exit

//RELOAD BASHRC FILE 
$ source .bashrc

//********************//
//NOTE: USAGE EXAMPLE //
//********************//
$ addcommit 'commit message here'

Of course, since I've included the -m option in the alias, the alias will need to be combined with an inline commit message. Since this is normally done by wrapping the message in single quotes, that doesn't change for the alias. You should be able to use this like this: addcommit 'commit message here'.

If you wanted to throw caution to the wind, you could even add, commit and push all at the same time.

Jump to Verbose Error Logging in Settings.php

If you're a Drupal Dev like me and you like to play with fire (on your localhost, of course), you probably hit the WSoD (White Screen of Death) every so often. One of the best tools to remedying a Drupal WSoD is to enable 'verbose error logging'; this usually requires inserting the following bit of PHP into your settings.php file:  $config['system.logging']['error_level'] = 'verbose';. I hit the WSoD often enough so that I keep this line of code commented out in my settings.php file--basically, the last line of the file. If I hit a problem, all I need to do is uncomment it.

As prepared as I am, though, it always seems to take forever to find my way to that line. Once again, aliases come to the rescue. Check this out: it'll open the file with Nano and jump to the *last* line at the same time. Actually, I don't know if there's a way to "jump to the last line" of a file while opening it with Nano. What I've actually done here is to tell Nano to jump to a line number so high that Nano will simply move to the end of the document by default. If you have a piece of code in a fixed location, you can also just specify the exact line. I'll call my alias, error:

$ sudo nano .bash_aliases
//ADD THIS LINE
alias error='nano +5000 /path/to/your/settings.php'

ctl+o to save, ctl+x to exit

//RELOAD BASHRC FILE
$ source .bashrc

If you wanted to get a little more specific with jumping to the "exact" location of your verbose error logging line, you could use Awk to search your settings.php file for that specific line, print a line number and then pass that line number to Nano. Here's a variation on this theme that does exactly that. Awk searches to match the phrase error_level and prints whatever line number that phrase appears in. Wrapping this Awk command in $() is basically like making it a variable; it's read inline with the nano command. Below, I've also told nano to place the cursor three bytes in from the beginning of whatever this line number is. Since the verbose error logging line is commented out with two slashes (i.e., //), setting the cursor three bytes in means all I need to do once the file opens is hit backspace twice. Using Awk to find the exact line number allows you to locate your error logging line anywhere within the file--and, you wouldn't have to worry about the line number changing. That would look like this:

//ADD THIS LINE
alias error='nano +$(awk '/error_level/{print NR}' /pathto/settings.php),3 /pathto/settings.php'

//RELOAD BASHRC FILE
$ source .bashrc

Other Aliases

Hopefully these simple examples help illustrate how there's a lot of room for creativity with bash aliases. Basically, any repetitive task requiring lots of keystrokes is game. Another task that comes to mind is using nslookup to check for DNS changes. I don't have an alias for that one yet, but the next time I need to use it I may just create one. Another idea I stumbled across recently was to use an alias allowing you to search your bash history with grep: this is a really good one as well--allowing you to search through past terminal commands based on key word. When it comes to bash aliases, the world is your stage, really!

There's an obvious curve to mastering the Linux command-line; it's easy to understand why people can get turned off of it. For me, personally, though, I think aliases take away some of the edge. There's a certain part of me (as a developer) that enjoys finding solutions to the challenges of complicated tasks. "Work smarter, not harder" is a personal mantra, and, to a certain extent, is a necessity for any dev who'd like to avoid hitting the bottle midday--I think. Bash aliases are definitely your friend in this regard.