Effective Terminal on OS X

Here’s a list of tips and tricks to make your OS X Terminal experience a bit more effective.

Quick access to the Terminal

In order not to be disctracted from your line of thoughts and leave the flow, you need to have a quick way of accessing your Terminal. That goes for other applications, and files, as well. I prefer to use Quicksilver myself, which pops-up a small UI with suggestions of what application, or file, to open. The UI is visible by pressing a hot-key. If you want to open the Terminal, you start writing the application name: ter and the Icon of the application and the name will be shown to you (in my case Terminal). When the right application icon is presented, you press Enter and the application is launched.

The QuickSilver application
QuickSilver UI

Quicksilver can do a whole lot of other things, I mainly use it for launching programs and occasionally to open recently opened files. Opening applications this way takes fractions of a second, compared to finding the application in the launch bar.

Bash completion

To work effectively from the command prompt you want to use completions when writing. For example, to navigate around in the file system with the cd command, you would press Tab to get the path filled in or get suggestions of available resources that matches what you have started to write.

OS X and bash comes with a number of these completions out of the box, but you can get even more support by installing bash_completions. One way of doing this is to install MacPorts, which according to their site is “an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system.”

After installing MacPorts, you can install bash completion by issuing:

sudo port install bash-completion

Then it needs to be activated, by pointing out the shell-script file from your .profile (or .bash_profile if you have one of those). Open the file ~/.profile in a text editor and add the lines:

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion

These settings are applied when you open up a new Terminal window. Depending on which file you have edited, you may have to logout and login again for the system to re-read the files.

One very nice example of the added completions is the ssh copy command scp that now have remote file completion support, that is if the command can access the server without having to prompt you for username/password. This can be achieved by using passwordless logins.

This means that you can write something like:

scp some.server.com:~/bac

and then press Tab to get completed with

scp some.server.com:~/backup-last-release

on the remote server. Isn’t that cool?

You can see a list of other programs that now have gotten bash completion support by listing the files in /opt/local/etc/bash_completion.d directory. Examples of supported programs are: Apache ant, ssh, MacPorts, perl, mysqladmin etc.

Bash completion for subversion

If use command-line subversion, installed using MacPorts, and want to add completions then you write:

sudo port install subversion +tools

And activate it by adding the following lines to your .profile file:

if [ -f /opt/local/share/subversion/tools/client-side/bash_completion ]; then
    . /opt/local/share/subversion/tools/client-side/bash_completion

Bash completion for Git

If you have installed git using MacPorts, you can get additional completions by writing:

sudo port install git-core +bash_completion

and then add the following lines to your .profile file:

if [ -f /opt/local/share/doc/git-core/contrib/completion/git-completion.bash ]; then
    . /opt/local/share/doc/git-core/contrib/completion/git-completion.bash

Reverse history search

If you often find yourself looking for previously written commands in the Terminal history by pressing the Up/Down arrow, you may speed up your search by activating the reverse-interactive-history-search using Ctrl + R. This will give you a prompt looking like:


Here you can find commands in the history by writing the first letters, such as cd which might give you:

(reverse-i-search)`cd': cd dev/myproject/target/classes

If that was the command you were looking for, press Enter to activate, or press Ctrl + R to find the next match. If you want to edit the command before activating it, you press Right arrow. Pressing Escape exits the reverse-i-search and returns to your normal prompt.

History ignore

If you still find yourself using the Up/Down arrows to navigate the history, you can make it more effective by specifying what should not appear in the history. For example, the ls command is probably faster to write than to find. Also the same command appearing three times in a row in the history is just annoying.

All of this can be controlled by setting the $HISTIGNORE variable. It takes a colon-separated list of ignores, where the & sign is a special construct for the previous history line. To ignore the ls command and those pesky duplicates, you write:

export $HISTIGNORE="&:ls"

or better yet, add the line to your .profile file to have it activated all the time.

Leave a Reply