Flex The Power of Your Mac Terminal: Aliases

Rob Conery | Opinion

Many developers own Macs as their primary development box, yet they don't fully flex the Unix OS that powers OSX. I was one of them - used to my Windows GUI and clickity-click experience. I've since changed that ...

##What's In Your .Profile?

First - I want to give a shout out to PeepCode, and their

excellent tutorial on the Advanced Commandline. Next, I want to say that what I'm about to show you is

not restricted to Macs.

You can use terminal tricks in every flavor of Unix - and you can also flex Powershell on Windows should you desire.

This post is about the Mac - if you have Powershell tricks please do share.

It all starts in your profile - or ".profile" to be specific. When the terminal loads it opens up a "shell" - a command surface over the raw, underlying Unix system. Typically, this program is called "bash" or the "Bourne Again Shell".

Some like to use other flavors (like "zsh") - but bash is the default so I'll assume you are running it.

Anyway - when bash starts up, it loads in a number of files - .bashrc, .bashhistory, .bashprofile etc. There are a number of them and you should probably leave most of them alone if you don't know quite what you're doing. The one we're concerned with today is ".profile".

You might be wondering - what's with the "." in front of the name? The main reason is that it signifies that the file or directory is a system-level one and you shouldn't mess with it if you're a casual user. You're not a casual user, so let's mess around...

First, open up the terminal. It's in /Applications/Utilities/Terminal.

Now, take a look around!


This command lists out the files. You'll see all the files in your "home" directory - which is usually your name on your Mac and can be found at "/Users/[your name]". But where are all those dotfiles? The answer - they're hidden... let's tell bash we want to see them.

ls -a

The "-a" is an option, and it means "show me all" the files. Now you can see your dotfiles. Do you see your ".profile" file in there? Sometimes it's not there, so let's create one:

touch .profile

The "touch" command updates the file time on a given file (last modified date) and will create it if it's not there. Let's take a look at it:

open .profile

This will open up your default text editor on the mac and you can now edit away. But what do you put in there? The short answer is that (just about) everything in Unix is a text file, and when you edit it you tend to use system commands to tell the system what to do at certain points.

This can be daunting - but we're programmers!

Programming Your System

OK the remedial stuff is out of the way - let's do something fun. We could put a load of commands inside our profile - but this is a system file and we don't really want to be customizing it a lot, so let's close the editor and setup some directories.

In the terminal, let's create a directory right in our root:mkdir ~/dots

The tilde/ might look weird - but it simply means "my home directory". This should be easy to remember if you're an ASP.NET dev. Now let's head into that directory:

cd ~/dots

Let's create a file for our aliases, then edit it:

touch aliases open aliases

Great! Let's make our first one. An alias is simply a shortname for a longer command. Let's make an alias that will open up our "aliases" no matter where we are and allow us to edit them. Put the following code into your aliases file:

alias aa='open ~/dots/aliases'

Now close the file - making sure to save it if prompted. We need to load that file into bash so we can execute the command, and we do it with the "source" command:

source ~/dots/aliases

Now type in "aa" and boom! You can edit your aliases! But typing "source" all the time is a bit silly - let's alter that alias now that we have it open:

alias aa='open ~/dots/aliases && . ~/dots/aliases'

I've chained two commands together by using the "&&" - just like I would programming in most popular languages. But where's the "source" command?

A system alias for "source" is "." - something you'll use quite often when working with a Unix system. [Rest of paragraph removed because I was wrong about a thing]

As a final step - let's make this automatic. That's where ".profile" comes in - it's loaded automatically every time you start up bash. Can you guess what we need to do?

open ~/.profile . ~/bin/aliases [exit]

We could be ninjas here if we wanted to and not even use an editor, instead we could tell Unix to push some text into a file for us:echo ".~/bin/aliases" >> .profile

Close and reopen the terminal - and you're off and running...

More Complicated Aliases

You're probably thinking of all kinds of fun aliases you can use at this point - but if there are more than two simple commands you probably want to create a function.

Functions work sort of in the way you expect - you create one, give it a name, and inside of it you execute all kinds of fun commands. Let's say you have a blog built with

Nesta CMS (what I use). It's a static blog without a database, so when you add a post (like this one) - you actually add a file.

You want to shorten the time it takes to create a post and open your blog. You can do this with a function:

function new_post(){ cd ~/blog touch ~/blog/content/pages/$1.haml mate ~/blog/content/pages/$1.haml }

This function will:*Change Directories into the root of your blog

*Create a new page based on the argument you send in

*Open up TextMate so you can edit your new fileWe could invoke the function like this:

new_post "flex-the-power-of-your-mac-terminal"

And boom - up pops your new post.

It's Wide Open

Git shortcuts, Mercurial, SSH, deployment - you name it. Aliases are a handy way to cut a ton of time off your development day.

Finally - for a comprehensive look at what you can do with the terminal,

head over to PeepCode and watch this video. It comes with a whole set of shortcuts and handy customizations that you can use straight away! A great deal at $9.