Article

Fixing your Apple Terminal

April 11th, 2013

I think most of you will agree that Apple's Terminal looks and behaves horribly out of the box. So we need to adjust a few things... By following this guide I hope you'll enjoy the Terminal a bit more :)

Save your settings

Whenever you change something in the Terminal's settings, you'll want to click "Shell""Use Settings as Default". This will make sure the next time you open the Terminal, things will still look and behave the same way you left them. I'm not going to repeat this, so remember it!

The look

Open the Terminal preferences and go to the tab "Startup". Behind "On startup, open" select the theme of your choice. Personally I like "Pro", which gives you the feel of the good old ascii console :)

Go to the tab "Settings" and again select the theme of your choice. Browse through the mini-tabs ("Text", "Window", etc) and adjust whatever you like. As for the font, I suggest you try "Inconsolata" at 12pt (and enable "Antialias text").

Keyboard behaviour

Something that irritates me a lot is that the commands "delete", "home", "end", "page up" and "page down" don't behave like expected. So we'll go ahead and fix that!

Open the Terminal preferences and go to the tab "Settings", then to the mini-tab "Keyboard". Change the following entries:

end               \033[4~
forward delete    \033[3~
home              \033[1~
page down         \033[6~
page up           \033[5~
shift end         scroll to end of buffer
shift home        scroll to start of buffer
shift page down   scroll to next page in buffer
shift page up     scroll to previous page in buffer

You'll need to copy-paste those first 5 entries to get the values in.

Next, open the file /etc/inputrc with your favorite editor (it probably doesn't exists yet). Insert the following and save the file:

"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": quoted-insert

And if you like tab-completion to be case-insensitive, you can also add:

set completion-ignore-case on

You'll need to reopen the Terminal for the new /etc/inputrc to kick in. Now you can enjoy key combinations like [fn] + [left arrow], which will correctly reach the terminal as "home".

Bash (and MacPorts

First off, I use MacPorts for packages that aren't native to OS X (or I'd like to upgrade / configure differently). I like Bash and Bash-completion, which can easily be enabled for a lot of packages in MacPorts. But what you're about to read applies to any shell you'd like to use.

In order to be able to use the Bash that comes with MacPorts, we'll have to allow it first. This can be done by adding it's path to /etc/shells. Open it with your editor, and add /opt/local/bin/bash (or the path of another shell you chose) to it.

Next we can set it as default shell for your user, and optionally the root user:

chsh -s /opt/local/bin/bash
sudo chsh -s /opt/local/bin/bash

If you get a message like "non-standard shell", you haven't setup /etc/shells correctly ;)

Last we need to tell the Terminal to use this shell. Open the Terminal preferences and go to the tab "Startup". Behind "Shells open with" select "Command (complete path)" and enter /opt/local/bin/bash -l.

Now your terminal will use the correct Bash installation (or whatever shell you choose).

More bash candy

Now we can adjust a few Bash settings. This part only applies to Bash, so if you've chosen another shell you can skip this part.

Open the file /etc/profile and make sure the following is in it:

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
fi

Then open the file /etc/bashrc. We're going to add a couple of things to it.

This will set a few defaults on ls, like colors, human readable sizes, etc. It also allows you to type ll to do a ls -l, and la to do a ls -Al:

alias la="/bin/ls -AFGhl"
alias ll="/bin/ls -FGhl"
alias ls="/bin/ls -FG"

This will enable Bash-completion for packages in MacPorts:

if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
    . /opt/local/etc/profile.d/bash_completion.sh
fi

I suggest you also edit the file /opt/local/etc/macports/variants.conf and add +bash_completion to it. This will enable Bash-completion for MacPorts packages by default.

You might want to set your favorite editor. Many applications will recognize this:

export EDITOR="/opt/local/bin/vim"

As you can see I choose Vim, which was installed with MacPorts.

Another nice touch is to change the prompt:

PS1='\u\[\e[0;32m\]@\[\e[m\]\h\[\e[0;32m\]:\[\e[m\]\W \[\e[0;32m\]\$\[\e[m\] '

This can be anything you want.

Vim

Like I said, I prefer Vim as editor. I've installed Vim through MacPorts, and added a custom configuration:

/opt/local/etc/vimrc

That about wraps it up!

Don't forget to click "Shell" → "Use Settings as Default" and reopen the terminal for everything to take effect.

Then start using the Terminal as a powerful tool for lots of tasks :)

Jasper N. Brouwer

Senior Software Developer