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.
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:
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.
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).
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:
Type open -a, followed by a space on the command line (don't press Return yet).
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
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:
Open a new Terminal window and type curl -O , with a space after the switch.
Bring up your web browser and navigate to http://www.oreilly.com.
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
Press Enter in the Terminal window to download header_main.gif to your computer.
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.
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:
Emacs mode is simpler; vi mode allows finer control.
Emacs mode allows you to yank cut text and set a mark; vi mode does not.
The command-history-searching capabilities of the two modes differ.
Table 1-2, Table 1-3, and Table 1-4 describe the various editing keystrokes available in 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. |
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. |
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. |
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.
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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.
Key command |
Description |
---|---|
Control-C |
Interrupts the process; cancels the previous command (-. 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 |
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 |
bindkey bindkey [option] bindkey [option] [key] bindkey [option] [key] [command] bindkey [option] [key] [string]
bindkey is a tcsh shell command that is used to select, examine, and define key bindings for use in the Terminal.
The following list describes the various uses of the bindkey command.
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 |
open file open [-a application] file open [-e] file
The open command can be used to open files and directories, and to launch applications from the Terminal application.
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
Copyright © 2003 O'Reilly & Associates. All rights reserved.