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:
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).
1.5.2. Working with File and Directory Names
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
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:
126.96.36.199. 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:
188.8.131.52. Emacs mode
Table 1-2. Cursor positioning commands (Emacs mode)
Table 1-3. Text deletion commands (Emacs mode)
Table 1-4. Command control (Emacs mode)
184.108.40.206. 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. Commands available (vi's insert and command mode)
Table 1-6. Editing commands (vi insert mode)
Table 1-7. Cursor positioning commands (vi command mode)
Table 1-8. Text insertion commands (vi command mode)
Table 1-9. Text deletion commands (vi command mode)
Table 1-10. Text replacement commands (vi command mode)
Table 1-11. Character-seeking motion commands (vi command mode)
220.127.116.11. 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
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 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 18.104.22.168, 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 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:
To open your /Public directory:
To open the /Applications directory:
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:
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.