To
customize the
shell
used by the Terminal, start by changing the
Terminal’s Preferences (Terminal
→ Preferences). In the
Preferences pane, you can tell the Terminal to execute the default
shell or a specific command (such as an alternative shell) at
startup.[3]
You can also declare the terminal type
($TERM
), which is set as xterm-color
by default. The other choices for the environment variable
TERM are ansi,
rxvt, vt52, vt100, vt102,
and xterm.
Among other things, the default setting for TERM allows you to take
advantage of the support for color output in ls
(via the -G
option) and color syntax highlighting in the
vim
editor.
You can also adjust the Terminal’s characteristics using Terminal → Window Settings (or
-I), which brings up the Terminal Inspector, shown in Figure 1-3. Table 1-1 lists the available window settings. Changing these settings affects only the topmost Terminal window. If you want to change the default settings for all future Terminal windows, click the Use Settings As Defaults button at the bottom of the Terminal Inspector window.
Table 1-1. Window settings
Pane |
Description |
---|---|
Shell |
Displays the shell used by the Terminal and lets you choose whether to close the Terminal window when the shell exits. |
Processes |
Displays the processes running under the frontmost window. You can
also control whether Terminal will warn you if you try to close the
window while you are running a program. You can disable this by
choosing Never under “Prompt before closing
window”. You can also supply a list of commands that
should be ignored, so if you’re running a program
(such as |
Emulation |
Controls the Terminal emulation properties. |
Buffer |
Sets the size and properties of the scrollback buffer. |
Display |
Changes the character set encoding, cursor style, font, and other attributes. |
Color |
Changes colors and transparency of the Terminal window. |
Window |
Controls window dimensions, title, and other settings. |
Keyboard |
Controls key mappings. |
One useful option available in the Emulation tab is
“Option
click to position
cursor”. If you enable this feature, you will be
able to Option-click with the mouse to position the cursor in
Terminal applications such as vim
or
Emacs
(this could save you many keystrokes when
you need to move the insertion point). This option also works over a
remote login session, assuming that this is supported by the remote
host’s terminal capabilities.
You can
customize the Terminal in shell scripts
using
escape sequences or AppleScript
commands. xterm
users may be familiar with using
the following command to set the
xterm
’s title when using the
bash
shell:
echo -n -e "\033]0;My-Window-Title\007"
or the following when using
tcsh
:
echo '^[]2;My-Window-Title^G'
Mac OS X’s Terminal accepts these sequences as well.
Tip
^[
is the ASCII ESC character, and
^G
is the ASCII BEL
character. (The BEL character is used to ring the terminal bell, but
in this context, it terminates an escape sequence.) The
escape
sequences described here are
ANSI escape sequences, which differ
from the shell escape sequences described earlier. ANSI escape
sequences are used to manipulate a Terminal window (such as by moving
the cursor or setting the title). Shell escape sequences are used to
tell the shell to treat a metacharacter, such as
|
, as a literal character rather than an
instruction to pipe standard output somewhere else.
To type the
^[
characters in bash
, use
the key sequence Control-V Escape (press Control-V and release, then
press the Escape key). To type ^G
, use Control-V
Control-G. The vim
editor supports the same key
sequence; Emacs
uses Control-Q instead of
Control-V.
You can capture the bash
escape sequence in a
function that you can include in your
.bash_profile
script:
function set_title ( ) { case $TERM in *term | xterm-color | rxvt | vt100 | gnome* ) echo -n -e "\033]0;$*\007" ;; *) ;; esac }
Then you can change the title by issuing the following command:
set_title your fancy title here
You may want to package this as a shell script and make it available to everyone who uses your system, as shown in Example 1-3.
Example 1-3. Setting the Terminal title in a shell script
#!/bin/bash # # Script settitle # Usage: settitle title # if [ $# == 0 ]; then echo "Usage: settitle title" else echo -n -e "\033]0;$*\007" fi
You can also use
osascript
to execute AppleScript commands that
accomplish the same thing:
osascript -e \ 'tell app "Terminal" to set custom title of first window to "Hello, World"'
Traditionally, Unix users tend to avoid spaces in file and directory names, sometimes by 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 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 (Mac OS 9 and earlier) 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\[RETURN] 2001/Microsoft\ Word
Or you can enter the path using quotes:
open -a /Volumes/"Mac OS 9"/"Applications (Mac OS 9)"/"Microsoft Office[RETURN] 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\[RETURN] 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
, and
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
(Mac OS
X’s System folder) is a valid completion for the
first three characters.
Although other shells are available
in Mac OS X, as we noted earlier, the default shell in Mac OS X
Panther is bash
. Earlier versions of Mac OS X
shipped with tcsh
as the default shell. Although
you can change the default shell in the Terminal preferences, this
does not affect the login shell used for remote or console logins. To
change your default shell in a more pervasive manner, see Section 3.7.5 in
Chapter 3. If you install additional shells on
the system, you’ll need to add them to the
/etc/shells
file to make Mac OS X aware that
they are legitimate shells.
[3] You can change the default shell in the Terminal preferences, but it will not affect the login shell used for remote or console logins. Changing a user’s default shell is covered later in this chapter.
Get Mac OS X Panther for Unix Geeks, 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.