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 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).

defaults

When you customize your Mac using the System Preferences, or an application’s preferences, all those changes and settings are stored in what’s known as the preferences system, and the command-line utility to change your preferences is the defaults command. 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. Another solution is to delete the preferences file for the application from ~/Library/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 Property List Editor 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 few basic examples of how to use the defaults command.

Examples

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

View all the user defaults on your system
% defaults domains

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

View the settings for your Dock
% defaults read com.apple.dock

This command reads the settings from the com.apple.dock.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.dock | more
Change the location of your Dock to the top of the screen

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

orientation = bottom;

You’ll see that its value is set to bottom, which means that your Dock is located at the bottom of the screen. To change that setting, try the following:

% defaults write com.apple.dock 
                           orientation top

After a short pause, you’ll be returned to another command prompt, but you’ll notice that the Dock is still located at the bottom of the screen. Unlike some other changes you make with the defaults command, changes to the Dock will take effect only if you log out and log back in.

Enter exit, and quit the Terminal, and then save any changes in other applications and quit them too. Now log out and log back in to your system (Apple Log Out). When you log back in, you’ll see the Dock in all its glory floating just below the menu bar at the top of the screen. To quickly change its location back to the bottom of the screen (or the left or right side), use Apple Dock Position on (Left, Bottom, or Right).

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

opens 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 .doc, Word v.X will launch. To open the file with Word 2001, you need to do the following:

% open -a /Volumes/Mac\ OS\ 9/Applications\ 
                        \(Mac\ [RETURN]
OS\ 9\)/Microsoft\ Office\ 2001/Microsoft\ 
                        Word [RETURN]
~/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 you need only tack on the path and filename for the file.

open -e [ filename_path ]

The -e option forces the use of 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/Navigator.app 
                        Sites/index.html

The -a option is used to launch Chimera, the Cocoa-compliant web browser, which is based on the Mozilla source code (hence its true application name, Navigator) for Mac OS X (assuming you have Chimera installed on your system, http://www.mozilla.org/projects/chimera) for viewing the index.html file, located in your Sites directory.

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\ [RETURN]
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/ [RETURN]
CoreServices/ Classic\ Startup.app'

Now to launch the Classic environment, simply 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/ [RETURN]
CoreServices/Classic\ Startup.app'

Get Mac OS X Pocket Guide, Second Edition 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.