Additional Shell Commands

One of the first things that traditional Unix users will notice when they start poking around in the Terminal is that there are a few new commands they’ll need to add to their repertoire. Three that we’ll discuss here are bindkey, defaults, and open.

bindkey

bindkey is a tcsh shell command, used to select, examine, and define key bindings for use in the Terminal. Table 4-13 shows the various uses of the bindkey command.

Table 4-13. Using the bindkey command

Command

Description

bindkey

List all of the key bindings

bindkey -c 
key cmd

Bind key to Unix command cmd

bindkey -d

Restore the default key bindings

bindkey -e

Change the key bindings to Emacs mode

bindkey key

List the bindings for key

bindkey 
key cmd

Bind key to editing command cmd

bindkey -l

List the editing commands and their meanings

bindkey -r key

Remove binding for key

bindkey -s 
key str

Bind key to string str

bindkey -u

Display a message, showing how to use the bindkey command

bindkey -v

Change the key bindings to vi mode

For additional information on key bindings and how to alter them, see Using csh & tcsh (O’Reilly & Associates, Inc., 1995).

defaults

When you customize your Mac using the System Preferences, all of those changes and settings are stored in what’s known as the defaults system. Everything that you’ve done to make your Mac yours is stored as XML data in the form of a property list (or plist). Your property lists are stored in ~/Library/Preferences.

Warning

Using the defaults command is not for the foolhardy. If you’re not comfortable with the command line or unsure of how to change a setting properly, you should stick to using the application’s Preferences pane, rather than trying to use the defaults command.

If you do manage to mangle your settings, the easiest way to correct the problem is to go back to that application’s Preferences pane and reset your preferences.

Every time you change one of those settings, that particular property list is updated. For the initiated, there are two other ways to alter the property lists. The first is by using the PropertyListEditor application (/Developer/Applications), and the other is by using the defaults command in the Terminal. Extensive coverage of both is beyond the scope of this book, but we’ll show you a basic example of how to use the defaults command.

Examples

The following are some examples of working with the defaults command:

View all of the user defaults on your system
% defaults domains

This will print a listing of all of the domains in the user’s defaults system. The list you’ll see are run together with spaces in between—not quite the prettiest way to view them.

View the settings for your Terminal
% defaults read com.apple.Terminal

This command reads the settings from the com.apple.Terminal.plist file, found in ~/Library/Preferences. This listing is rather long, so you might want to pipe the output to less or more to view the contents one screen at a time:

% defaults read com.apple.Terminal | more
Change your Terminal’s opaqueness so you can see through it

Near the end of that listing, look for the following:

TerminalOpaqueness = 1.00;

You’ll see that its value is set to 1.00, or 100%—a non-see-through Terminal. To change that setting, try the following:

% defaults write com.apple.Terminal 
                           TerminalOpaqueness 0.75

After a short pause, you’ll be returned to another command prompt. Enter exit, and close that Terminal window, then open a new Terminal window to see your new semitransparent window. The value we’ve given sets the opaqueness to 75%.

For additional options and to learn more about how to use the defaults command, enter defaults -help or view the defaults manpage (man defaults).

open

With Mac OS X, you can launch any application from the command line using the open command. There are three ways to invoke the command:

open [ filename ]

This will open the file and its associated application if it isn’t already running. For example:

% open textFile.txt

would open the file textFile.txt using the default text editor, which is TextEdit.

open -a [ application_path ] [ filename ]

The -a option lets you specify the application to use when opening the file. For example, let’s say you have both Microsoft Office 2001 and Office v.X on your system and you want to open a Word file using Word 2001. If you use open filename, Word v.X will launch. To open the file with Word 2001, you need to do the following:

% open -a /Volumes/Mac\ OS\ 9.2.2/Applications\ 
                        \(Mac\ OS\ 9\)/Microsoft\ Office\ 2001/Microsoft\ 
                        Word ~/Documents/filename.doc

While that might look ugly (and it is), the command does work. In this case, Classic would also launch because Word 2001 is a Classic app.

Note

There is a shortcut for inserting long pathnames like the one shown in this example: locate the application in the Finder, and drag the application icon from the Finder window to the Terminal window after typing open -a at the command line. The path for the application will be inserted after the command, and then all you need to do is tack on the path and filename for the file.

open -e [ filename_path ]

This will open the file using the TextEdit application. For example:

% open -e ~/Books/Templates/proposal_template.txt

Some additional examples of using the Terminal to open files and launch applications are shown here:

Open an HTML page using a browser other than Internet Explorer?

The other way to do this is to specify the application, using the -a option:

% open -a /Applications/Mozilla/Mozilla.app 
                        Public/mypage.html

The -a option is used to launch Mozilla (assuming you have Mozilla installed on your system, http://www.mozilla.org) for viewing mypage.html, located in your Public folder.

Launch Classic from the Terminal?

If you find that you’re using the Classic environment, one way you can launch Classic from the Terminal is with the following:

% open /System/Library/CoreServices/Classic\ 
                        Startup.app

And while that does the trick, a faster way to do this is to set up an alias in the shell. To do this, enter the following on the command line:

% alias classic 'open -a /System/Library/ 
                        CoreServices/ Classic\ Startup.app'

Now all you need to do to launch the Classic environment is to type classic on the command line and hit return.

This assumes you’re running tcsh as the default shell. If you’re running bash, use the following to set up the classic alias:

$ alias classic='open -a /System/Library/ 
                        CoreServices/Classic\ Startup.app'

Get Mac OS X Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.