Book HomeMac OS X for Unix GeeksSearch this book

1.5. Using the tcsh Shell

The tcsh shell offers advanced command-line editing capabilities, filename completion, and a variety of customizations. Although tcsh is the default user shell, Mac OS X 10.2 (Jaguar) uses the Bourne shell (/bin/sh) for handling scripts (such as those found under the /etc directory), and we suggest you do the same. However, tcsh is a fine user shell, and this section explains how to customize it and take advantage of some of its features.

1.5.1. Customizing the tcsh Shell

You can customize tcsh by changing certain environment variables, by creating aliases for frequently used commands, or by binding keys to commands (see "bindkey" in Section 1.5.4, later in this chapter).

If you want to make your customizations permanent (so you don't have to issue the commands each time you log in), put the appropriate commands in one of tcsh's startup files in your home directory. Here are some of the startup files into which you can put these commands:

.tcshrc
This script is executed each time you launch a shell. When you open a Terminal window, .tcshrc is executed. If you start a sub-shell (or run a csh shell script), .tcshrc will again be executed. If your .tcshrc contains recursive definitions, consider moving those commands to .login. For example, if you issue the command set path = ( $HOME/bin $path ) in your .tcshrc, then $HOME/bin will get prepended to your PATH environment variable each time you launch a sub-shell.

.login
This script is executed each time you launch a login shell, which includes opening a new Terminal window or logging in remotely. The .login script runs after .tcshrc. The .login file should contain settings that should only be applied once, such as PATH settings. The .login script is guaranteed to be run only once, regardless of how many sub-shells you invoke under a single login shell.

.logout
This script is run when you exit a login shell, but not when you exit a sub-shell.

The following listing shows what happens when you initially launch a shell, start a sub-shell, and exit the sub-shell and login shell:

Last login: Fri Jul 19 19:24:52 on ttyp1
Welcome to Darwin!
Running .tcshrc script
Running .login script
[Brian-Jepsons-Computer:~] bjepson% tcsh
Running .tcshrc script
[Brian-Jepsons-Computer:~] bjepson% exit
exit
[Brian-Jepsons-Computer:~] bjepson% exit
logout
Running .logout script

There are system-wide versions of these scripts, which are invoked before the scripts in a user's home directory: /etc/csh.login, /etc/csh.cshrc, and /etc/csh.logout.

WARNING: tcsh will also read commands from the .cshrc fikle, if present, but if you have both a .tcshrc file and a .cshrc file, it will ignore .cshrc. For example, Fink (see Chapter 6) instructs you to insert a path in .cshrc, but if you've already got a .tcshrc file, that's where you should put the command instead.

Mac OS X borrows a handful of scripts from MIT's Project Athena to help simplify this configuration. On Mac OS X, sample tcsh configuration files are kept in the /usr/share/tcsh/examples directory. In particular, this directory contains .tcshrc, .login, and .logout files named as rc, login, and logout. To use these configurations, create .tcshrc, .login, and .logout files that invoke the sample ones. This can be done by adding the following lines to your .tcshrc, .login, and .logout files:

source /usr/share/tcsh/examples/rc        # put this in ~/.tcshrc
source /usr/share/tcsh/examples/login     # put this in ~/.login
source /usr/share/tcsh/examples/logout    # put this in ~/.logout

Once this is done, you can then customize the tcsh environment by creating a ~/Library/init/tcsh directory. You can create individual files in this directory for each type of customization.

aliases.mine
Contains shell aliases.

completions.mine
Contains command-line completions.

environment.mine
Defines environment variables.

rc.mine
Specifies run commands.

path
Defines the command search path.

The startup scripts in /usr/share/tcsh/examples will use these files, if they exist. Of course, you can simply use a .tcshrc file in your home directory. (The system-wide /etc/csh.cshrc script will be read first.) For more information on tcsh, see Using csh and tcsh (O'Reilly).

1.5.2. Working with File and Directory Names

Traditionally, Unix users tend to avoid spaces in file and directory names, sometimes inserting hyphens and underscores where spaces are implied, as follows:

textFile.txt
text-file.txt
text_file.txt

However, most Mac users tend to insert spaces into their file and directory names, and in a lot of cases, these names tend to be long and descriptive. While this practice is okay if you're going to work in the GUI all the time, it creates a small hurdle to jump over when you're working on the command line. To get around these spaces, you have two choices: escape them, or quote the file or directory name.

To escape a space on the command line, simply insert a backslash (\) before the space or any other special characters, such as a parenthesis. Because they have meaning to the shell, special characters that must be escaped are: * # ` " ' \ $ | & ? ; ~ ( ) < > ! ^. Here is an example of how to use a backslash to escape a space character in a file or directory name:

cd ~/Documents/Editorial\ Reports

Or you can use quotation marks around the file or directory name that contains the space, as follows:

cd ~/Documents/"Editorial Reports"

There is one other way to get around this problem, but it involves using the Finder in combination with the Terminal application. To launch a Classic application such as Word 2001, which probably lives on the Mac OS 9 partition of your hard drive, you could enter the path as follows, using escape characters:

open -a /Volumes/Mac\ OS\ 9/Applications\ \(Mac\ OS\ 9\)/Microsoft\ Office\
   2001/Microsoft\ Word 

Or you can enter the path using quotes:

open -a /Volumes/"Mac OS 9"/"Applications (Mac OS 9)"/"Microsoft Office
   2001"/"Microsoft Word"

As you can see, neither way is very pretty, and both require you to know a lot of detail about the path. Now for the easy way:

  1. Type open -a, followed by a space on the command line (don't press Return yet).

  2. Locate Microsoft Word in the Finder and then drag its icon to a Terminal window to insert the path after the space. When you do this, the spaces and any other special characters will be escaped with backslashes, as follows:

    open -a /Volumes/Mac\ OS\ 9/Applications\ \(Mac\ OS\ 9\)/Microsoft\ Office\ 2001/
        Microsoft\ Word 
  3. Press Return to invoke the command and launch Word 2001. If Classic isn't already running, Classic will start, too.

You can also drag and drop URLs from a web browser, which can be used with curl -O to download files from the command line. For example:

  1. Open a new Terminal window and type curl -O , with a space after the switch.

  2. Bring up your web browser and navigate to http://www.oreilly.com.

  3. Drag the image at the top of the page to the Terminal window. You should now see the following in the Terminal window:

    curl -O http://www.oreilly.com/graphics_new/header_main.gif
  4. Press Enter in the Terminal window to download header_main.gif to your computer.

1.5.2.1. Tab completion

If you want to type a long pathname, you can cut down on the number of keystrokes needed to type it by using tab completion. For example, to type /Library/StartupItems, you could type /Li<tab>, which gives you /Library/. Next, type S<tab>. This time, instead of completing the path, you're given a choice of completions: Screen Savers/Scripts/StartupItems/. Type a little bit more of the desired item, followed by a tab, as in t<tab>. The full key sequence for /Library/StartupItems is /Li<tab>St<tab>.

If you have multiple completions where a space is involved, you can type a literal space with \<space>. So, to get a completion for /System Folder (the Mac OS 9 system folder), you should use /Sy<tab>\<space><tab>. It stops just before the space because /System (the Mac OS X system folder) is a valid completion for the first three characters.

1.5.3. Command-Line Editing with tcsh

Mac OS X's default shell, tcsh, lets you move your cursor around in the command line, editing the line as you type. There are two main modes for editing the command line, based on the two most commonly used text editors, Emacs and vi. Emacs mode is the default; you can switch between the modes with the following commands:

bindkey -e      Select Emacs bindings
bindkey -v      Select vi bindings

The main difference between the Emacs and vi bindings is that the Emacs bindings are modeless (i.e., they always work). With the vi bindings, you must switch between insert and command modes; different commands are useful in each mode. Additionally:

1.5.3.1. Emacs mode

Table 1-2, Table 1-3, and Table 1-4 describe the various editing keystrokes available in Emacs mode.

Table 1-2. Cursor positioning commands (Emacs mode)

Command

Description

Control-B

Moves the cursor back (left) one character.

Control-F

Moves the cursor forward (right) one character.

Esc then B

Moves the cursor back one word.

Esc then F

Moves the cursor forward one word.

Control-A

Moves the cursor to the beginning of the line.

Control-E

Moves the cursor to the end of the line.

Table 1-3. Text deletion commands (Emacs mode)

Command

Description

Del or Control-H

Deletes the character to the left of the cursor.

Control-D

Deletes the character under the cursor.

Esc then D

Deletes the next word.

Esc then Delete or Esc then Control-H

Deletes the previous word.

Control-K

Deletes from the cursor to the end of the line.

Control-U

Deletes the entire line.

Control-W

Deletes everything to the left of the cursor.

Control-Y

Yanks the previously deleted string.

Table 1-4. Command control (Emacs mode)

Command

Description

Control-P

Recalls the previous command from history.

Control-N

Recalls the next command from history.

Up arrow

Recalls the previous command from history.

Down arrow

Recalls the next command from history.

File-fragment Tab

Performs command-line completion. file-fragment can be a filename, a directory, or an executable in your $PATH.

cmd-fragment Esc then P

Searches history for cmd-fragment, which must be the beginning of a command.

cmd-fragment Esc then N

Like Esc then P, but searches forward in the history.

Esc num

Repeats the next command num times.

1.5.3.2. vi mode

vi mode has two submodes, insert and command mode. The default mode is insert. You can toggle between the modes by pressing Esc. Alternatively, in command mode, typing a (append) or i (insert) will return you to insert mode.

Table 1-5 through Table 1-11 describe the editing keystrokes available in vi mode.

Table 1-5. Commands available (vi's insert and command mode)

Command

Description

Control-P

Recalls the previous command from history.

Control-N

Recalls the next command from history.

Up arrow

Recalls the previous command from history.

Down arrow

Recalls the next command from history.

Table 1-6. Editing commands (vi insert mode)

Command

Description

Control-B

Moves the cursor back (left) one character.

Control-F

Moves the cursor forward (right) one character.

Control-A

Moves the cursor to the beginning of the line.

Control-E

Moves the cursor to the end of the line.

Delete or Control-H

Deletes the character to the left of the cursor.

Control-W

Deletes the previous word.

Control-U

Deletes from the beginning of the line to the cursor.

Control-K

Deletes from the cursor to the end of the line.

Table 1-7. Cursor positioning commands (vi command mode)

Command

Description

h or Control-H

Moves the cursor back (left) one character.

l or Space

Moves the cursor forward (right) one word.

w

Moves the cursor forward (right) one word.

b

Moves the cursor back (left) one word.

e

Moves the cursor to the end of the next word.

W, B, E

Has the same effect as w, b, and e, but treats whitespace as a word separator instead of any non-alphanumeric character.

^ or Control-A

Moves the cursor to the beginning of the line (first non-whitespace character).

O

Moves the cursor to the beginning of the line.

$ or Control-E

Moves the cursor to the end of the line.

Table 1-8. Text insertion commands (vi command mode)

Command

Description

a

Appends new text after the cursor until Esc is pressed.

i

Inserts new text before the cursor until Esc is pressed.

A

Appends new text after the end of the line until Esc is pressed.

I

Inserts new text before the beginning of the line until Esc is pressed.

Table 1-9. Text deletion commands (vi command mode)

Command

Description

x

Deletes the character under the cursor.

X or Delete

Deletes the character to the left of the cursor.

dm

Deletes from the cursor to the end of motion command m.

D

Deletes from the cursor to the end of the line (similar to issuing d$).

Control-W

Deletes the previous word.

Control-U

Deletes from the beginning of the line up to the cursor.

Control-K

Deletes from the cursor to the end of the line.

Table 1-10. Text replacement commands (vi command mode)

Command

Description

cm

Changes the characters from the cursor to the end of motion command m until Esc is pressed.

C

Has the same effect as c$.

rc

Replaces the character under the cursor with the character c.

R

Replaces multiple characters until Esc is pressed.

s

Substitutes the character under the cursor with the characters typed until Esc is pressed.

Table 1-11. Character-seeking motion commands (vi command mode)

Command

Description

fc

Moves the cursor to the next instance of c in the line.

Fc

Moves the cursor to the previous instance of c in the line.

tc

Moves the cursor just after the next instance of c in the line.

Tc

Moves the cursor just after the previous instance of c in the line.

;

Repeats the previous f or F command.

,

Repeats the previous f or F command in the opposite direction.

1.5.3.3. Additional command-line keys

As we've just illustrated, the tcsh shell offers dozens of special keystroke characters for navigation on the command line. Table 1-12 lists some additional command-line keys for use in either Emacs or vi editing mode.

Table 1-12. Additional key commands for the tcsh shell

Key command

Description

Control-C

Interrupts the process; cancels the previous command (Figure -. works as well).

Control-D

Signals end-of-input for some programs and returns you to the shell prompt. If Control-D is issued at a shell prompt, it will terminate the shell session and close the Terminal window, if you've set your Terminal preferences to close the window when the shell exits.

Control-J

Has the same effect as pressing the Return (or Enter) key. Hitting Control-J after issuing a command will invoke the command, or it will take you to the next line in the shell if no command was given.

Control-K

Removes everything to the right of the insertion point.

Control-L

Clears the display.

Control-Q

Restarts the output after a pause by Control-S.

Control-S

Pauses the output from a program that's writing to the screen.

Control-T

Transposes the previous two characters.

Control-Z

Suspends a process. To restart the process, issue the bg or fg command to place the process in the background or foreground, respectively.

Esc then C

Capitalizes the word following the insertion point.

Esc then Esc

Completes the name if only a partial pathname or filename is entered.

Esc then L

Changes the next word to all lowercase letters.

Esc then U

Changes the next word to all uppercase letters.

Tab

Has the same effect as pressing the Esc key twice.

1.5.4. 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. Two that we'll discuss here are bindkey and open. The defaults command is described in Chapter 8.

bindkey

Syntax

bindkey
bindkey [option]
bindkey [option] [key]
bindkey [option] [key] [command]
bindkey [option] [key] [string]

Description

bindkey is a tcsh shell command that is used to select, examine, and define key bindings for use in the Terminal.

Options

The following list describes the various uses of the bindkey command.

bindkey
Lists all of the key bindings.

bindkey -c key cmd
Binds key to the Unix command cmd.

bindkey -d
Restores the default key bindings.

bindkey -e
Changes the key bindings to Emacs mode.

bindkey key
Lists the bindings for key.

bindkey key cmd
Binds key to the editing command cmd.

bindkey -l
Lists the editing commands and their meanings.

bindkey -r key
Removes the binding for key.

bindkey -s key string
Binds key to the string string.

bindkey -u
Displays a message, showing how to use the bindkey command.

bindkey -v
Changes the key bindings to vi mode.

For example, to create a binding between the F2 key and the proto.term script from Section 1.3.1.1, earlier in this chapter, use this command:

bindkey -c ^[OQ 'open ~/Documents/proto.term'

To get the key sequence ^[OQ, type Control-V followed by the function key you want to bind, in this case F2. Now, any time you type F2 in the first Terminal window, it will open a new Terminal window using the settings saved in the proto.term file. You can put bindkey commands in your .tcshrc or .login script to make them permanent. For additional information on key bindings, and how to alter them, see Using csh & tcsh (O'Reilly).

open

Syntax

open file
open [-a application] file
open [-e] file

Description

The open command can be used to open files and directories, and to launch applications from the Terminal application.

Options

-a application
Uses application to open the file.

-e file
Forces the use of Mac OS X's TextEdit application to open the specified file.

Examples

To open a directory in the Finder, use open, followed by the name of the directory. For example, to open the current directory, type:

open .

To open your /Public directory:

open ~/Public

To open the /Applications directory:

open /Applications

To open an application, you need only its name. For example, you can open Project Builder (/Developer/Applications) with this command:

open -a "Project Builder"
TIP: You are not required to enter the path for the application, only its name--even if it is a Classic application. The only time you are required to enter the path is if you have two different versions of applications with similar names on your system.

You can also supply a filename argument with the -a option, which would launch the application and open the specified file with that application. You can use this option to open a file with something other than the application with which it's associated. For example, to open an XML file in Project Builder instead of the default text editor, TextEdit, you could use the following command:

open -a "Project Builder" data.xml

To open multiple files, you can use wildcards:

open *.c

To force a file to be opened with TextEdit, use -e:

open -e *.c

The -e switch will only open files in the TextEdit application; it cannot be used to open a file in another text editor, such as BBEdit. If you want to use TextEdit on a file that is owned by an administrator (or root), open -e will not work. You'll need to specify the full executable path, as in:

% sudo /Applications/TextEdit.app/Contents/MacOSextEdit filename

Enabling the root User

By default, the Mac OS X root user account is disabled, so you have to use sudo to perform administrative tasks. Even the most advanced Mac OS X users should be able to get by with sudo, and we suggest that you do not enable the root user account. However, if you must enable the root user account, start NetInfo Manager (/Applications/Utilities), click the lock to authenticate yourself, and select Enable Root User from the Security menu.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.