Linux User's Guide to Mac: Terminal

By Timothy R. Butler | Posted at 10:37 PM

Few things in Unix match the importance of the terminal emulator. Having a nice GUI is fine, but nothing beats the command line, which some have said is the "front line" of computing. As an official member of the Brotherhood of the Commandline, I have always kept at least one terminal window open at all times, and often three or more.

In Mac Land, that's Terminal. Yes, I could run the X server and use Xterm, but that would be a step backward from the one thing which drove me away from Unix in the first place. I came to Mac because I didn't have to use the X server to get a GUI, since Mac has the best one out there. Whatever I needed to do on the command line requires making Terminal do it right.

By default, Terminal has some rather odd behavior for someone coming from Unix, and more if your Unix is Linux. Most of it can be changed. One thing you will not get is the mouse-paste. That's unique to the X server. Sure, you can get a mouse driver for Logitech or Intellimouse which will let you alter the wheel button behavior, but it breaks other, more versatile and useful behavior, so you might as well get used to Cmd+C and Cmd+V. But most other things can be adjusted to behave as we Unix people have come to expect.

For this demonstration, I'm using Joe as the application which will most readily lend itself to explaining what we need to do. Joe's Own Editor offers a quick and easy method to editing the configuration file to add non-default keystrokes. A couple of years ago I wrote a HOWTO on customizing Joe's keystrokes based on work by Anne Baretta and a few tips garnered here and there. In our last installment, we covered installing Joe and Aspell, so I'm assuming you either have them already, or can grasp what follows without them.

First, grab a copy of your system joerc for your $HOME folder (using the command line, of course):

cp /usr/local/etc/joe/joerc ~/
cp /usr/local/etc/joe/ftyperc ~/

If you have the latest version of Joe, you'll probably want to modify the ftyperc for your own use, as I do. But first, we have to understand the fundamental difference between Mac's Terminal and your generic Xterm. Terminal has some different default actions assigned to some of the keystrokes. More, as my HOWTO encourages assigning F-keys to Joe actions, you'll need to consider doing something I did: turn them off in the Mac GUI.

For serious Mac users, this is probably anathema. For Unix users, having all or most of the F-keys available for the applications is pretty important. In Mac's System Preferences, go to Keyboard & Mouse, and open Keyboard Shortcuts. For me, the simplest answer was to en masse turn off "Keyboard Navigation" shortcuts because I use the mouse for those things, if at all. Then I added "Dock, Expose and Dashboard" because I can't imagine why I would want them. Your mileage may vary, but if you want the F-keys for Terminal, consider it.

In my HOWTO on Joe's keystrokes, I point out the method of determining what keycodes are output by your terminal emulator of choice, by hitting CTRL+V, followed by any other key or combination with modifiers. This works in Terminal. However, you'll notice they all include the "escape sequence" of ^[ which represents hitting the ESC, but forms a part of what Joe needs to interpret custom keystrokes. However, when you open the Preferences dialog in Terminal, choose a profile (I set Pro as my default), and click on the "Keyboard" setting page, you'll see most of them are in the pattern of 33xxxx. If you understand that 33 is equivalent to ^[, you can do this.

Now, scanning down through the list of default keystrokes in this dialog, we see why the "gray keys" won't work without holding down the SHIFT key. Change those assignments. You'll need to assign them to a "string" instead of any of the optional actions. Just select any of the gray key names and then click "Edit" and select the option to send a string. Below are various strings as they are output in most Xterms, which is what most command line applications are expecting, even when compiled on Mac. I have broken down the spacing as Joe uses the Xterm output, according to the instructions in my HOWTO.

Keystroke Joe format Mac label Terminal format
Right ^[ [ C cursor right 33[C
CTRL+RT ^[ [ 1 ; 5 C control cursor right 33[1;5C
ALT+RT ^[ [ 1 ; 3 C option cursor right 33[1;3C

In order to tell Terminal to output that second command as I wished, I had to remove the default and tell it to send the string in the last column of the second row above. Furthermore, I have to warn you the dialog will attempt to interpret any deleting keystrokes, so neither DEL nor BKSP can be used to clear a string in that dialog. You'll need to highlight with the mouse, then either overtype or paste what you want there.

Below are all the arrow keys and gray keys except the HELP and DEL keys on Mac keyboards. Use those you need. In some cases, you'll have to use the + button the dialog interface to add the keystrokes not included by default, along with any modifiers.

Keystroke Joe format Mac label Terminal format
Left ^[ [ D cursor left 33[D
CTRL+LF ^[ [ 1 ; 5 D control cursor left 33[1;5D
ALT+LF ^[ [ 1 ; 3 D option cursor left 33[1;3D
UP ^[ [ A cursor up 33[A
CTRL+UP ^[ [ 1 ; 5 A control cursor up 33[1;5A
ALT+UP ^[ [ 1 ; 3 A option cursor up 33[1;3A
DN ^[ [ B cursor down 33[B
CTRL+DN ^[ [ 1 ; 5 B control cursor down 33[1;5B
ALT+DN ^[ [ 1 ; 3 B option cursor down 33[1;3B
HOME ^[ [ H home 33[H
CTRL+HOME ^[ [ 1 ; 5 H control home 33[1;5H
ALT+HOME ^[ [ 1 ; 3 H option home 33[1;3H
END ^[ [ F end 33[F
CTRL+END ^[ [ 1 ; 5 F control end 33[1;5F
ALT+END ^[ [ 1 ; 3 F option end 33[1;3F
PGUP ^[ [ 5 ~ page up 33[5~
CTRL+PGUP ^[ [ 5 ; 5 ~ control page up 33[5;5~
ALT+PGUP ^[ [ 5 ; 3 ~ option page up 33[5;3~
PGDN ^[ [ 6 ~ page down 33[6~
CTRL+PGDN ^[ [ 6 ; 5 ~ control page down 33[6;5~
ALT+PGDN ^[ [ 6 ; 3 ~ option page down 33[6;3~

Once you have told Terminal what keystrokes you want added and the strings to send, you are ready to edit your joerc according to the HOWTO linked above. Note that the ftyperc is where you put your specific options regarding various filetypes. For plain text (.txt), I use the following format options:

Text file. *.txt
-wordwrap
-tab 3
-indentc 32
-istep 1
-spaces
-purify
-rmargin 72
-french

I use the same for HTML files. The explantion for each one can be found in the joerc file.

There is one more peculiarity I've found in Terminal. For some strange reason, F1-F3 tend to output something different than what Joe expects, and probably most other command line programs from Unix. In my joerc, instead of using the standard .k1, I had to pickup the code output from Terminal as revealed by the command CTRL+V: ^[OP. Then, I placed this in my joerc where I wanted to use F1, after spacing it out properly. I did the same for F2 and F3.

After you've added your custom keystrokes and commands to joerc, make sure to change the "include" statement for your ftyperc (approximately line 360). It must be the full path. Mine says this:

:include /Users/edhurst/.ftyperc

Which brings up the last point of renaming them both by adding a period to the front of the file so they are read as configuration files:

mv joerc .joerc
mv ftyperc .ftyperc

After modifying the Terminal profile keystrokes, I tested a few other applications and they seemed to work okay. Enjoy.

Ed Hurst is Associate Editor of Open for Business.