Search the Catalog
Learning Unix for Mac OS X

Learning Unix for Mac OS X

By Dave Taylor, Jerry Peek
May 2002
0-596-00342-0, Order Number: 3420
156 pages, $19.95 US $30.95 CA £13.95 UK

Chapter 4
Customizing Your Session

One of the great pleasures of using Unix with Mac OS X surrounding it is that you get the benefit of a truly wonderful graphical application environment and the underlying power of the raw Unix interface. A match made in heaven!

This chapter discusses how to customize your Terminal environment both from the graphical user interface using Terminal ➝ Preferences and from the Unix shell by using shell configuration files.

Launching Terminal

Launch Terminal and you have a dull, uninspiring white window with black text that says `Welcome to Darwin!" and a shell prompt. But that's okay. We can fix it.

Changing Terminal Preferences

To change the display preferences in the Terminal application, go to the Terminal menu and choose Preferences.... You see a display similar to Figure 4-1.

Figure 4-1. Startup Preferences

 

Along the window's top, notice that a number of different preferences are configurable: Startup, Shell, Window, Text & Colors, Buffer, Emulation, and Activity. The icons suggest what each does, but let's have a closer look anyway, particularly since some of these settings definitely should be changed in our view. It's worth pointing out that these changes affect new windows not the current window.

Startup Preferences

When you first open Terminal Preferences, the Startup Preferences are displayed, as shown in Figure 4-1. The default behavior is to launch a new blank shell window each time the program is started, so you're ready to start typing commands immediately. Unless you're an advanced user, don't change this behavior. Instead, let's look at what's on the other preference screens, which you can view by clicking on each icon at the top of the Preferences window.

Shell Preferences

As Figure 4-2 reveals, there are a number of behavioral choices you can make on this panel, including whether you want each window to have its own login shell associated with your account, or whether all Terminal windows should run a specified shell (in this case, /bin/tcsh). In Mac OS X you're probably the only person who uses your computer, so if your login shell is acceptable to you, leaving the shell setting to reference the login shell is your best bet.

Figure 4-2. Shell Preferences

 

Also on this panel you can specify that when a login shell exits, the Terminal application can close the window, close the window only if the shell exited cleanly (that is, returned a nonzero status code, which means that all the applications gracefully shut down), or never close the window. If you like to study what you've done and want to be forced to explicitly close the Terminal window, the last choice is for you. Otherwise, either of the first two will work fine.

Finally, you can specify a nonstandard string encoding if you're working with an unusual language or font. By default, the UTF-8 (Unicode 8-bit) encoding is quite acceptable and will also keep you out of trouble with more complex character sets that Unix might not understand correctly.

Window Preferences

If you have a large display, or are running at a higher resolution than 800x600, you'll find it quite helpful to enlarge the Terminal window to offer a bigger space within which to work. Our default is 80 characters wide by 40 lines tall, as shown in Figure 4-3.

Figure 4-3. Window Preferences

 

The title of each Terminal window can be fine tuned too: you might find the device name (what you'd get if you typed tty at the shell prompt) and the window size particularly helpful.

TIP:   If you want to change the Terminal window title at any point, you can use the Set Title option by either choosing it from the Shell menu or typing c-Shift-T. The resulting Terminal Inspector window, as shown in Figure 4-4, lets you fine tune the appearance of an existing window (by contrast, the preferences only affect future windows, not those that are already open).

Figure 4-4. Terminal Inspector window

 

Text & Colors Preferences

The area that you'll probably fine tune more than any other is Text & Colors. Here you can specify a different (or larger) font, and change the foreground, background, bold, cursor, and selection colors, as well as define the shape of your cursor within the Terminal window. The default is black text on a white background, but we find that light text on a dark background is easier to read for extended periods. One suggested preference is where the background is a very dark blue, the cursor is yellow, normal text is light yellow, bold text is light green, and the selection is a dark green.

While you can choose any font available on your system, you'll find that your display is much more comprehensible if you stick to monospace or fixed-width type. Monaco is a very good choice, and is the default typeface for the Terminal application.

Buffer Preferences

The settings in this area probably don't need changing. The scrollback buffer allows you to scroll back and review earlier commands and command output. The default value of 10,000 lines should be more than enough for most people. If you want to use less memory, you can put in a smaller number or completely disable the scrollback buffer, rather than specifying a size.

You can also choose whether the Terminal should wrap long lines (not all Unix programs will wrap long lines, and so they might disappear off the edge of the window if this option isn't set), or whether you should automatically jump to the bottom of the scroll buffer upon input, if you've scrolled back to examine something that transpired earlier in your session. These options are set by default, and you should probably leave them that way.

Emulation Preferences

These preferences don't need to be altered by most users. If you're using the Emacs text editor, however, you will want to select "Option (alt) key acts as meta key" to allow meta-key sequences. What's a meta-key sequence? Emacs uses lots of Control, Alt, Control-Alt, and similar key sequences for complex commands, and one modifier it uses is a new-to-Mac-users "meta" key. If you use Emacs, you'll see when the meta key is necessary.

TIP:   Some Titanium PowerBook G4s have an inordinate delay before emitting audio, and if you've got one of these and you've noticed it as a problem, select "Mute terminal bell" to neatly sidestep the problem. This also has the nice side effect of preventing people around you knowing when you've made a mistake too, of course.

Otherwise, it's best to leave "translate newlines to carriage returns" so that you can ignore the difference in end-of-line sequences in Mac files versus Unix files, and to avoid strict "vt100" emulation, because it can get in the way of some of the newer Mac OS X Unix utilities. Whether or not you enable Option-Click for positioning the cursor might depend on whether you're a Unix purist (for whom the "good old keyboard" works fine) or whether you're trying to simplify things. Beware that if you do enable Option-Click positioning, it won't work in all cases, only when you're in a full-screen application such as Emacs.

Activity monitor

One of the more subtle capabilities of the Terminal application is that it can keep track of what applications you're running so it can be smart about confirming window close requests: if there's something still running in the window, it'll pop up a dialog box asking if you're sure you want to quit. This feature is very helpful if you are prone to accidentally click the wrong window element or push the wrong key sequence. We recommend you enable the activity monitor to help avoid accidentally closing windows (and automatically killing all the processes running within those windows).

Customizing Your Shell Environment

The Unix shell reads a number of configuration files when it starts up. These configuration files are really shell programs, so they are extraordinarily powerful. Shell programming is beyond the scope of this book. For more detail, see Paul DuBois' book, Using csh and tcsh (O'Reilly). Because Unix is a multiuser system, there are two possible locations for the configuration files: one applies to all users of the system and another to each individual user.

The system-wide setup files that are read by tcsh, the default shell for Mac OS X, are found in /usr/share/init/tcsh. You only have permission to change these system-wide files if you are logged in as root. However, you can create an additional file called .tcshrc in your home directory that will add additional commands to be executed whenever you start a new Terminal window. (If you configure Terminal to use another shell, such as the Bourne shell, the C shell, or the Z shell, you'll need to set up different configuration files, which we don't discuss.) The system-wide setup files are read first, then the user-specific ones, so commands in your .tcshrc file may override those in the system-wide files.

The .tcshrc file can contain any shell command that you want to run automatically whenever you create a new Terminal. Some typical examples include changing the shell prompt, setting environment variables (values that control the operation of other Unix utilities), setting aliases, or adding to the search path (where the shell searches for programs to be run). A .tcshrc file could look like this:

set prompt="%/ %h% "
setenv LESS 'eMq'
alias desktop "cd /Users/taylor/Desktop"
date

This sample .tcshrc file issues the following commands:

By default, the .tcshrc file doesn't exist in your home directory. Only the system-wide configuration files are read. But if you create the file, it will be read and its contents executed the next time you start a shell. (You can also create a file called .logout to contain commands to be executed when you end a shell.) You can create or change these files with a text editor, such as vi (see ). Don't use a word processor that breaks long lines or puts special nontext codes into the file. Any changes you make to these files will take effect the next time you log in (or, in some cases, when you start a new shell--such as opening a new Terminal window). Unfortunately, it's not always easy to know which shell setup file you should change.[1] And an editing mistake in your shell setup file can keep you from logging in to your account! We suggest that beginners get help from experienced users, and don't make changes to these files at all if you're about to do some critical work with your account, unless there's some reason you have to make the changes immediately.

You can execute any customization command we show you here from the command line as well. In this case, the changes are in effect only until you close that window or quit Terminal.

For example, to change the default options for less so it will clear the Terminal window before it shows each new page of text, you could add the -c option to the LESS environment variable. The command would look something like this:

% setenv LESS 'eMqc'

(If you don't want some of the less options we've shown, you could leave those letters out.) Unix has many other configuration commands to learn about; the sources listed in Chapter 10 can help.

Just as you can execute the setup commands from the command line, the converse is true: any command that you can execute from the command line can be executed automatically when you log in by placing it in your setup file. (Running interactive commands such as vi or ftp from your setup file isn't a good idea, though.)

Changing Your Prompt

The easiest customization you can perform is to change your prompt. By default, tcsh on Darwin has a shell prompt made up of your computer hostname, your account name, and a percent sign (for example: [dsl-132:~] taylor%). If you'd rather have something else, it's time to edit your own .tcshrc file. Use the vi editor (you might need to flip back to ) to create a file called .tcshrc in your home directory (/Users/yourname) and add the following to the end of the file: set prompt="% ". You can also change the prompt for a single session by invoking the command as follows:

[dsl-132:~]taylor% set prompt="% "
%

This command will give you a simple, spare % prompt with nothing else. (The % is traditional for shells derived from the Berkeley Unix C Shell, while $ is traditional for shells derived from the original Bell Labs Bourne Shell.) It's not necessary--you could use a colon, a greater than sign, or any other prompt character--but it is a nice convention, because it will immediately tell an advanced user what kind of shell you are using.

If that's all you could do to set your prompt, it wouldn't be very interesting, though. There are a number of special character sequences that, when used to define the prompt, cause the shell to print out various bits of useful data. Table 4-1 shows a partial list of these special character sequences for fine tuning your prompt.

Table 4-1: Favorite percent escape sequences for tcsh prompts

Value

Meaning

%/

The current working directory

%~

The current working directory, with your home represented as ~ and other users homes represented as ~user

%c

The trailing element of the current working directory, with ~ substitution

%h

The current command history number

%M

The full hostname

%m

The hostname up to the first dot

%B/%b

Start/Stop bold mode (matches the bold color in the Terminal color preferences)

%t

Time of day in 12-hour (a.m./p.m.) format

%T

Time of day in 24-hour format

%n

The username

Experiment and see what you can create that will meet your needs and be fun too. For many years, a popular Unix prompt was:

% set prompt="Yes, Master? "

It might be a bit obsequious, but on the other hand, how many people in your life call you "Master"?

One prompt sequence that we like is:

% set prompt="%/ %h% "

This prompt sequence shows the current working directory, followed by a space, and the current history number, and then a percent sign to remind the user that this is csh or tcsh. For example, the prompt might read:

/Users/taylor 55%

This tells me immediately that /Users/taylor is my current directory, and that I'm on the 55th command I've executed. (Because you can use the arrow keys to scroll back to previous commands, as described in the section, , this is no longer as important, but there is a very powerful command history syntax built into tcsh that allows you to recall a previous command by number. If you're familiar with this syntax, making the command history number part of the prompt can be handy.) On multiuser systems, it's not a bad idea to put the username into the prompt as well, so you always know who the system thinks you are.

Creating Aliases

The flexibility of Unix is simultaneously its greatest strength and downfall; the operating system can do just about anything you can imagine (the command-line interface is certainly far more flexible than Aqua, the graphical interface!) but it's very difficult to remember every single flag to every command. That's where shell aliases can be a real boon. A shell alias is a simple mechanism that lets you create your own command names that act exactly as you desire.

For example, we really like the -a flag to be included every time we list a directory with ls, so we created an alias:

% alias ls      "/bin/ls -a"

This indicates that each time we type ls in the shell, the /bin/ls command is going to be run, and it's going to automatically have the -a flag specified. To have this available in your next session, make sure you remember to add the alias to your .cshrc file too.

You can also have aliases that let you jump quickly to common locations, a particularly helpful trick when in Mac OS X:

% alias desktop "cd /Users/taylor/Desktop"

There are many aliases predefined in the tcsh shell. You can list active aliases all by typing alias without any arguments:

% alias
.       pwd
..      cd ..
cd..    cd ..
cdwd    cd `pwd`
cwd     echo $cwd
desktop cd /Users/taylor/Desktop
ff      find . -name !:1 -print
files   find !:1 -type f -print
l       ls -lg
line    sed -n '!:1 p' !:2
list_all_hostnames      grep -v "^#" /etc/hosts
ll      ls -lag !* | more
ls      /bin/ls -F
pp      winname prosperpoint;ssh -l taylor prosperpoint.com;winname Darwin
staging winname staging;ssh -l taylor staging.intuitive.com;winname Darwin
term    /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
winname echo -n '^[]0;!^^G'
word    grep !* /usr/share/dict/web2

Have an alias you really want to omit? You can use unalias for that. For example, unalias ls would remove the -a flag addition. Drop the unalias command into your .cshrc file and it'll ensure that the system alias won't bother you ever again.

Further Customization

There's not much more you can do with the Terminal application than what's shown in this chapter, but there's an infinite amount of customization possible with the tcsh shell (or any other shell you might have picked). To learn more about how to customize your shell, read the manpage. Be warned, though, the tcsh manpage is over 5,800 lines long!

Oh, and in case you're wondering, manpages are the Unix version of online help documentation. Just about every command-line (Unix) command has a corresponding manpage with lots of information on starting flags, behaviors, and much more. You can access any manpage by simply typing man cmd. Start with man man to learn more about the man system.

For more information on customizing tcsh, see Paul DuBois' book, Using csh and tcsh, or Unix Power Tools, Second Edition, by Jerry Peek, Tim O'Reilly, and Mike Loukides, both available from O'Reilly.


1. In addition to .tcshrc, the shell will also read and execute files called .login, .cshrc, and .logout. Some files are read by login shells and others by non-login shells. Some are read by subshells, others aren't. Some terminal windows open login shells, others don't. And if you're using the Bourne shell, still other files, such as .profile, are read instead. We focus only on .tcshrc here, but a more advanced Unix book can provide more information once you need it.

Back to: Learning Unix for Mac OS X


oreilly.com Home | O'Reilly Bookstores | How to Order | O'Reilly Contacts
International | About O'Reilly | Affiliated Companies | Privacy Policy

© 2001, O'Reilly & Associates, Inc.
webmaster@oreilly.com