Improve your Workflow with Terminal Aliases

Confession time, I’m awful at the command-line. Used to waste nearly an hour every week either mistyping or looking up Terminal commands for common development tasks. That was over a billable week a year lost. Not anymore, I’ll show you how I use Terminal aliases to increase productivity on the command-line and improve workflow.

Terminal aliases are shortcuts for longer commands. Here’s an example of how using them has saved me a lot of time.

One thing I regularly have to do is flush my DNS cache. In Mt. Lion here’s the command:

> sudo killall -HUP mDNSResponder

Pretty easy to remember right? I forget this command all the time despite using it nearly every week. What I want to do is type in something simple like flush_dns instead. Here’s how I do it.

First I open up Terminal and navigate to my home directory

> cd ~

Next look for any hidden files resembling .profile or .bash_profile.

> ls -a

Screen Shot 2014-09-03 at 9.15.01 PM

Next I open up .bash_profile for editing.

> sudo nano .bash_profile

Then its a simple addition to the file to set my terminal alias

alias flush_dns=sudo killall -HUP mDNSResponder

Screen Shot 2014-09-03 at 9.16.17 PM

Then I just load Terminal so it will recognize the new alias.

> . ~/.bash_profile

Nice! No more looking up that stupid command, Terminal does it for me. It even makes sure that it has the sudo command before it.

Speaking of sudo one thing I run into constantly is forgetting to add it when I modify a system file, like /etc/hosts. Easy enough with an alias. I can create a command called “edit_hosts” which will do that for me.

Screen Shot 2014-09-03 at 9.23.32 PM

Another thing I struggle with is remembering where the MAMP httpd-vhost.conf file is. I get about as far as /Applications/MAMP/conf and then I think, what comes next? Is it “extra” or “extras”, oh wait no it’s “apache”, “httpd-vhosts.conf” or “httpd-vhosts.conf”? I don’t have time for that, now it’s just:

alias edit_vhosts='sudo nano /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf'

While I’m at it, why not add a shortcut for editing the profile and reloading it?

alias edit_profile='sudo nano ~/.bash_profile'
alias reload_profile='. ~/.bash_profile'

Also how about php’s config file?

alias edit_php='sudo nano /Applications/MAMP/bin/php/php5.4.10/conf/php.ini'

Now let’s add some commands to help me navigate to some common directories

alias goto_projects='cd /Projects'
alias goto_mamp='cd /Applications/MAMP'
alias goto_htdocs='cd /Applications/MAMP/htdocs'

Finally the next big thing I always run into is compressing and uncompressing folders & files. I can never remember the tar commands to do it. Now I can just call pack and unpack.

alias pack='sudo tar -zcvf'
alias unpack='sudo tar -zxvf'

These are just a few ideas for terminal aliases you can use, they work for me. But the important thing is you can customize the aliases to fit your workflow, your system. Terminal aliases are just one tool you can use to start making your tools work for you instead of against you.

Got some cool aliases you’ve used? Let me know in the comments!