Esoteric Tip #4: Custom ANSI colors in Terminal.app

Posted: January 27th, 2011 | Author: | Filed under: Esoteric Tips | 1 Comment »

In this second Esoteric Tip, I focus on a way to improve the way that Terminal.app handles ANSI colors.

There are a set of standard escape sequences that most terminals support and that correspond to the same actions. If you’re really interested in all the gruesome history of the standard, I encourage you to read the Wikipedia page on the subject. Of particular interest in this post are commands of the form “color the text a certain color starting now”. A program can issue these commands to the terminal as it prints; this is what commands like the ridiculously useful colordiff use to output to the terminal in color and emphasize things that it thinks need emphasizing. Unfortunately, while Terminal.app understands these color commands, there’s no way of customizing the colors used, even though there is support for customizing background and text color. This is particularly annoying if you have a custom Terminal color scheme (which I do, more on that in a minute) and your background is, say, gray; dark green on gray looks terrible.

The only way to fix this at the moment is to use SIMBL and essentially hack Terminal.app to do what you want. There’s a lovely little SIMBL plugin call TerminalColours that enables changing what colors map to what color strings.

I like color schemes that are easy on the eyes (understand, I spend a lot of time looking at terminals and text editors) and I’ve grown particularly fond of the Zenburn color scheme. I use it in Emacs all the time, and now I can use it in Terminal.app as well. I found a Zenburn Terminal.app theme, but it didn’t customize ANSI colors and thus looked pretty awful on color terminals.

Blech.

After installing SIMBL and the TerminalColours plug-in, I rooted around in the Zenburn color scheme file that I use for Emacs to determine the right colors to use. Here they are for convenience, in the form “Color: Red, Green, Blue”

Normal Colors Light Colors
Black: 0, 0, 0
Red: 204, 147, 147
Green: 127, 159, 127
Yellow: 224, 207, 159
Blue: 140, 208, 211
Magenta: 220, 140, 195
Cyan: 147, 224, 227
White: 220, 220, 204
Black: 112, 144, 128
Red: 220, 163, 163
Green: 143, 178, 143
Yellow: 240, 223, 175
Blue: 148, 191, 243
Magenta: 236, 147, 211
Cyan: 147, 224, 227
White: 255, 255, 255

After modifying the colors with the handy menu provided by TerminalColour, the terminal is actually readable:

Update: You can download my modified Zenburn terminal theme here (right-click + Save As works best).


Esoteric Tip #3: Fixing Terminal.app’s Arrow Keys

Posted: January 27th, 2011 | Author: | Filed under: Esoteric Tips | Comments Off

I spend a lot of time on the command-line, mostly for running experiments and running various scripts over log files. OS X’s Terminal.app has gotten a lot better in the last few years, but there are a couple of things that don’t quite work out of the box. The next two tips in the Esoteric Tips series focus on fixing some of those quirks and making Terminal.app a little more usable.

Ctrl+Arrow Keys

I spent a couple years working on a Linux box during the day, so I got used to a common feature of Gnome’s terminals: if I held down the Ctrl key and pressed the arrow keys, I would go forward and backward by a word. This is especially useful, for example, if you just executed ./foo.py dir1 var2 var3 and want to execute ./foo.py dir2 var2 var3; just hit the up arrow to go to the previous command in your history (I’m assuming bash here), hit Ctrl + left arrow a couple times to go back two words, change dir2 to dir1. Fewer keystrokes, takes less time, and your hands hurt less. Key combinations like this send an escape sequence to the terminal, typically octal 33 (the Escape key, or from the terminal’s point of view “treat the next character(s) as a command”) followed by a character or two indicating what action to perform. The terminal looks up the character in a table and performs the appropriate action (in our case, move the terminal’s cursor left or right one word).

If you are used to this working, switching to Terminal.app will be a bit of a shock because the escape sequence sent when Ctrl+arrow keys are pressed doesn’t do the right thing; instead, it inserts a capital C or a capital D wherever your cursor happens to be at the time. So that previous example would leave my command line looking like this:

./foo.py dir1 var2 var3CDCDCD

This is pretty irritating. Fortunately, there’s an easy fix.

In Terminal, go to Preferences. In the Settings tab, there’s a sub-tab called Keyboard that contains a mapping of key combinations to the control characters they output. Here’s what that window looks like:

The two keyboard combinations you care about are “control cursor left” (highlighted in the above picture) and “control cursor right”. First, select “control cursor left” and click “Edit”. You’ll get a dialog box that looks like this:

Select the text in the text box under Action (that currently contains “\033[5D”) and press the Escape key followed by the ‘b’ key. The dialog box should look like this:

Click OK.

What you’ve just done is told Terminal.app to send the Escape key (octal 33) followed by the character ‘b’ whenever you press Ctrl+left arrow, which has the effect of moving the terminal cursor to the left one word.

Do a similar sequence of steps for control cursor right, except enter Escape + f instead of Escape + b. Your preferences window should look like this:

Now, Ctrl+left/right arrow should do what you (well, at least what I) expect them to do.