Terminal 101: Creating Aliases for Commands

Every Monday (or in this case, Tuesday), we'll show you how to do something new and simple with Apple's built-in command line application.

Every Monday (or in this case, Tuesday), we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!

While using Terminal, you may discover that you constantly run a string of commands so often, it's ridiculous that you have to type them in at all. Well, just as you can create shortcuts to applications and automate actions in OS X, you can also do so for Terminal commands that are in constant rotation. These are called aliases, and you can set them up to run temporarily or permanently through your Mac’s command line interface.

Creating a Temporary Alias

To create a temporary alias that is good for the current terminal session, you will use the following format to create the alias:

alias new_name='command to be performed'

Just about any command can be used to create an alias, but we'll use this sample command throughout this how to. This command will let us jump to our Documents folder just by typing in "documents":

alias documents='cd /Documents'

Once the command has been assigned to its alias, you can type in the alias name followed by the enter key to perform the action assigned to the alias.

Recording Aliases Permanently

If you assign an alias using the above method, then it will only be available for the current terminal session. Once you close the terminal window, the alias will no longer be available. To make the alias permanent, you will need to edit a file called “.bash_profile” by running the following command:

nano /.bash_profile

This will open nano (a command line text editor). Use the arrow keys to place the cursor at the bottom of the file, and type in the aliases that you wish to use, one per line, just as you did when creating temporary aliases. When you are done, press Control + O (Write Out) to save the file. Press Command + X to exit nano and go back to the command line.

When you quit and re-open the Terminal application, your aliases will work without having to re-create them each time.

Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.