Aliases in UNIX allow you to customize the command line by creating commands of your own devising. A single alias can contain multiple built-in commands strung together to perform a complex task, or just provide a shortcut for an often-used command that you'd rather not type over and over.

Here are some tips to help you use aliases to increase your productivity and save time and typing.

1. Launching Applications from Terminal

If you find yourself spending a lot of time in Terminal, it feels faster to keep typing than to use the mouse or trackpad to switch to or launch a new application. Luckily, with a minimal amount of UNIX goodness, there's an app a command for that.

You can actually launch any OS X application from Terminal by typing open -a and the full path to the application (including its .app file extension). For example:

Last login: Wed Aug 12 00:02:29 on ttys004
Welcome to Cerberus...

Cerberus:~ bantik$ open -a /Applications/Calculator.app

This will open (and/or switch to) the Calculator app, just as if you had double-clicked it in the Finder. The meaning of open is pretty obvious; the -a flag means that you're opening an application, not a file; finally, /Applications/Calculator.app is the UNIX (actually, POSIX) path to the Calculator application.

So let's add some aliases to allow fast app switching from Terminal. First, start a fresh Terminal window. Then, assuming you're using the Bash shell, type:

Cerberus:~ bantik$ vi .bash_profile

(Don't be afraid of vi. It can smell your fear.)

To put the document in edit mode, press i (for insert, indicating that you plan on inserting text). Then enter one line in the file for each alias you want to create. Each line consists of the name of alias (what you will be typing it in Terminal) and a UNIX command, in this case the command for opening an application. You'll have to put in the full path, and escape any spaces with a \ character. (Hint: The easiest way to get the path of a file in Finder is to drag it to a Terminal window.) Be sure to add the .app extension to the end of the name of the application.

alias www='open -a /Applications/Safari.app'
alias preview='open -a /Applications/Preview.app'
alias sql='open -a /Applications/Sequel\ Pro.app'
alias css='open -a /Applications/CSSEdit.app'

Note that there is no space between the name of your alias, the equal sign, and the command-line string inside the single-quotes.

When you're done, press esc followed by :wq, which signifies your intention to write your changes and then quit.

Now your aliases are saved and will be ready for you in your next Terminal session, but not the current one-- the .bash_profile file is read at the beginning of a given Terminal session, so you'll have to open a new tab or window to test your aliases.

Some applications may well surprise you, in that they will accept command-line arguments. For example, if you create an alias for Safari (as the example above shows), you can actually open a web page directly from Terminal by typing

Cerberus:~ bantik$ www http://www.idolhands.com/

The same thing applies for Preview; just pass it the name of the file you want to open, and it just works. Slick.

2. Add Aliases for your Project Folders

Why stop with applications? If you have multiple projects that you're constantly working on, add an alias for each one and save yourself from having to cd through various levels of directories to get to your project folder. For example, let's say that each of your Rails projects is in a distinct folder in ~/Documents/projects/. You could set up your aliases like this:

alias projects='cd ~/Documents/projects/'
alias hello='cd ~/Documents/projects/hello_world/'
alias bbrails='cd ~/Documents/projects/bbrails_toolkit/'

3. Invoke MySQL with a Particular Database

My development partner, Rod Monje, devised this time-saver that can be invoked when you're in the folder for a Rails application:

alias my='mysql -u root $DB'

That little gem will bring you into MySQL, pre-selecting the appropriate database for your current Rails app.

4. Create Mini Man Pages

If you're constantly having to look up the syntax for a particular UNIX command, and a little hint would be better than digging through a MAN page, create an alias to remind you of the syntax:

alias scp?='echo "scp -C user@source:/path/to/file [user@destination]:/path/to/destination"'

For this example, to get to your reminder, just type scp?:

Cerberus:~ bantik$ scp?
scp -C user@source:/path/to/file [user@destination]:/path/to/destination
Cerberus:~ bantik$

I've really only scratched the surface with aliases. Think of them as a way to DRY your command-line experience: any time you notice yourself typing the same sort of thing over an over (opening an SSH connection to a production server, perhaps?), consider making an alias.

Related Articles


Comments

David Bourguignon
August 13, 2009 at 11:38 PM

Great tips, but you can do a: open -a Calculator It works too. I think it will look at /Applications and subfolders (and $HOME/Applications, ...)
Leave a Comment


IdolHands.com Spam-o-MeterTM
Bot
Spammer
Moron
Human






* Required fields.