Get to Know (and Customize) Terminal - Take Control of the Mac Command Line with Terminal (2015)

Take Control of the Mac Command Line with Terminal (2015)

Get to Know (and Customize) Terminal

As I mentioned in What’s Terminal?, the application you’re most likely to use for accessing the command line in Mac OS X is Terminal. Since you’ll be spending so much time in this application, a brief tour is in order. In addition, you may want to adjust a few settings, such as window size, color, and font, to whatever you find most comfortable and easy to read.

Learn the Basics of Terminal

The moment has arrived. Find the Terminal application (inside the folder /Applications/Utilities), double-click it, and take a Zen moment to contemplate the emptiness (Figure 1).

**Figure 1:** The Terminal window harks back to pre-graphical days.

Figure 1: The Terminal window harks back to pre-graphical days.

To state the obvious, it’s a (mostly) empty window. A Terminal window simply shows a command-line interface generated by a shell (in this case, the bash shell). As long as you’re in this window, you can largely forget about your mouse or trackpad: with a couple of notable exceptions (see the sidebar Using a Mouse in Terminal), everything you do here uses the keyboard only.

Of course, the window isn’t completely empty. The first line lists, by default, the date and time of your last login. In this example, it’s:

Last login: Sun Mar 15 14:09:46 on ttys000

That last part, on ttys000, is a bit of esoteric information that signifies the terminal interface with which you logged in the last time. It might say something different (such as on console) or nothing at all—for all practical purposes, you can safely ignore this line.

The second line is the actual command line (the line on which you type commands):

MacBook-Pro-15:~ jk$ █

The rectangular box at the end (which may instead appear as a vertical line or an underscore, any of which may or may not blink) is the cursor (not to be confused with the pointer, which reflects mouse movement). Everything before the cursor is known as the prompt, which is to say it’s prompting you to type something.

The first part of the prompt, MacBook-Pro-15, is the name of my Mac (by default, spaces are replaced with hyphens, and punctuation, if any, usually disappears). The colon (:) is simply a visual separator. Next is the tilde (~), which signifies that I’m currently in my home directory (which, for me, is /Users/jk). The jk is the short username of the account under which I’m logged in. And finally, the $ signifies that I’m logged in as an ordinary (non-root) user. (I say more about the $ in the sidebar The $, #, and Other Strange Things on My Command Line, ahead.) If your short username is cindy and your computer’s name, as shown in System Preferences > Sharing, is Cindy’s Groovy iMac, your command line may look something like this:

Cindys-Groovy-iMac:~ cindy$ █

All these things are customizable; see Customize Your Profile.

Using a Mouse in Terminal

Although you’re never required to use a mouse or trackpad in Terminal—and all command-line programs were designed to be used with only a keyboard—there are a few situations in which a pointing device can come in handy:

· You can use your mouse to select text (for copying, say), just as you would in any other Mac app.

· You can drag a file or folder in from the Finder to copy its path to the command line, formatted in such a way that you don’t have to worry about any space characters (read Get the Path of a File or Folder).

· In the nano text editor, you can Option-click to move your cursor to that spot (or the nearest valid location).

· You can Command-double-click a URL on the command line to open it in your default browser.

· Starting in 10.10 Yosemite, you can scroll (for example, with a two-finger vertical swipe on a trackpad, or with a scroll wheel on a mouse) through man (manual) pages, and move the cursor up or down by line (just as if you pressed Up arrow or Down arrow repeatedly) in programs such as the nano text editor.

Modify the Window

The window you’re looking at is just like any other Mac OS X window. You can move it, minimize it, resize it, zoom it, scroll through its contents, and hide it using the usual controls. So please do adjust it to your liking. However, I want to make two important points about window modification:

· First, resizing isn’t only a good idea, it’s practically mandatory. Some commands you run in this window will generate a lot of text, including some large tables, and you’ll find it much easier to work in the command line if your Terminal window is a bit bigger. Go ahead and make the window as large as you want—but do leave at least a bit of space so that you can see some parts of other windows on your screen.

· Second, any changes you make to the window ordinarily last only until you close it. If you open a new window—or quit Terminal and launch it again later—you’re returned to the defaults. So, once you get your Terminal window to a size, shape, and position you like, choose Shell > Use Settings as Default. Thereafter, all new Terminal windows you open use your preferred characteristics. (I say more about customizing windows ahead, in Change the Window’s Attributes.)

Open Multiple Sessions

Most applications can have multiple windows open at once—think of your word processor, your Web browser, or your email program, for example. The same is true of Terminal—you can have as many windows open as you need, each with its own command line. To open a new window, press Command-N.

When you open a new window in Terminal, you begin a new session. That means another copy of the shell runs, separate from the first copy. You can run a program or navigate to a location in the first session, and run a completely different program or navigate to another location in the second. The two sessions don’t normally interact at all; it’s as though you’re using two different computers at once that happen to share the same set of files.

Why would you want to do this? Perhaps you want to refer to a program’s man (manual) page in one window, while trying out the command in a second. Perhaps one shell is busy performing some lengthy task and you want to do something else at the same time. Or perhaps you want to compare the contents of two directories side by side. Whatever the case, remember: you’re not limited to using one window—or one session—at a time.

But wait, there’s more! Every window in Terminal also supports multiple tabs—just like most Web browsers (Figure 2). So if you want to have multiple sessions open without the screen clutter of multiple windows, you can do so easily. Create a new tab by pressing Command-T. Exactly as in a browser, you can drag tabs to rearrange them, close them individually, and even drag a tab from one window to another.

**Figure 2:** Terminal windows can have multiple tabs, which can be moved and closed individually just like those in most Web browsers.

Figure 2: Terminal windows can have multiple tabs, which can be moved and closed individually just like those in most Web browsers.

Change the Window’s Attributes

Moving and resizing windows is one thing, but Terminal lets you go further. You can change the background color (and transparency), font (typeface and size), text color, cursor type, and numerous other settings. In fact, you can change far more attributes than I care to describe here, so I want to explain just a few of the basics.

For starters—just to get a feel for what’s possible—choose Shell > New Window (or New Tab) and try some of the prebuilt themes. For example, choose Shell > New Window > Homebrew for a display with bright green text in 12-point Andale Mono against a slightly transparent black background. Or choose Shell > New Window > Grass for pale yellow text, in bold 12-point Courier, on an opaque green background, with a red cursor.

Figure 3 shows several examples.

**Figure 3:** Terminal windows can take on many themes; this image shows several of the stock themes. (The exact appearance depends on which version of Mac OS X you’re running.)

Figure 3: Terminal windows can take on many themes; this image shows several of the stock themes. (The exact appearance depends on which version of Mac OS X you’re running.)

If you prefer to use one of these other themes as your default, open a new window with that theme and choose Shell > Use Settings as Default. But you can also modify these themes or create your own.

To modify your window’s appearance, follow these steps:

1. Choose Terminal > Preferences and click Settings on the toolbar.

2. Select a theme in the list to modify it. Or, to create your own new theme based on an existing one, select a theme and choose Duplicate Settings from the pop-up action menu at the bottom of the list—or click the plus button to add your own theme from scratch.

3. To modify the text that appears in the window of the currently selected theme, click Text. A few of the more useful options in this view are the following:

§ Font: To change the typeface or size, click the Change button, select a new font, size, and style from the Fonts palette, and close the palette. For best results, I strongly recommend choosing a fixed-width (monospaced) font, such as Courier, Monaco, or Lucida Console.

§ Text color: To change the color of the font, click the color button to the left of the word Text and chose a color using the Colors palette. You can pick a separate color for boldface text and for text you’ve selected with the mouse by clicking the color buttons next to Bold Text and Selection, respectively.

§ Cursor attributes: To change the shape of the cursor, select the Block, Underline, or Vertical Bar radio button. Check Blink Cursor if you want it to blink, and if you want to change the cursor’s color, click the color button next to the word Cursor.

4. To modify the window itself, click Window. Some options you can change here include:

§ Title bar elements: To change the name of the window (“Terminal” by default), type new text into the Title field. You can also select any or all of the checkboxes beneath to display other information in the title bar, such as the name of the active process or the window’s dimensions. Terminal windows express their size in terms of rows and columns of text rather than in pixels. By default, Terminal windows are 24 rows by 80 columns, a size that harks back to old-style text-only terminals.

§ Background color: Click the color button under Background to open the Colors palette, in which you can choose a background color for the window. You can also adjust the opacity of the background color. Why would you want a partially transparent window? I like transparency because I can put a Terminal window directly above, say, a Web page and read instructions through the window as I type in Terminal! To adjust the opacity, move the Opacity slider at the bottom of the Colors palette.

§ Window size: You can change the default window size for the current theme by typing numbers into the Columns and Rows fields, or you can simply resize the window to your liking later by dragging the resize control at the window’s lower-right corner.

Note: My preference for window appearance is based on the Ocean theme (white text on a blue background) but with a larger window (160 columns by 50 rows) and background transparency set to 80%.

5. To make a particular theme the default (which means it’s used automatically when you launch Terminal, and when you press Command-N), select it and click the Default button beneath the list of themes. When you’re finished adjusting window settings, close the Settings window.

All the settings you change here take effect immediately for existing windows using the selected theme, and for the next new window or tab opened using that theme.

Set a Default Shell

As I explained in the introduction, this book covers only the bash shell, which has been the default since Mac OS X 10.3 Panther, though your account may have a different default if you migrated your account forward from an older system (even if you’ve gone through several upgrades since then). So you may want to confirm that you’re running bash, or switch to bash if not.

Find Out Which Shell You’re Using

To find out which shell is currently running, enter this:

echo $0

The shell replies with its own name, sometimes preceded by a hyphen:

-bash

Change Your Default Shell

If you want to change the default shell only for yourself, leaving other users’ defaults intact, follow these steps:

1. Open System Preferences > Users & Groups. (If you’re using Lion or earlier, open System Preferences > Accounts.)

2. If the lock icon in the lower left of the window is closed, click it and enter your administrator’s credentials to authenticate.

3. Right-click (or Control-click) on your name in the list on the left, and choose Advanced Options from the contextual menu.

4. In the dialog that appears, choose a different shell from the Login Shell pop-up menu.

5. Click OK, and then close System Preferences.

Although the Advanced Options pane warns that you need to restart your computer to apply changes, changing the default shell takes effect with the next Terminal session you open.

Change the Default Terminal Shell

To change the default shell Terminal opens regardless of which user is logged in or what that user’s individual preference is, do the following:

1. Choose Terminal > Preferences and click Startup on the toolbar.

2. Next to Shells Open With, select Command (Complete Path) and make sure the path to bash (/bin/bash) is filled in. (To use a different shell, such as zsh, substitute that shell’s name for bash.)

The setting applies starting with the next session you open.

The $, #, and Other Strange Things on My Command Line

By default, when you open a Terminal window, you see a prompt that ends in a $ (followed by the cursor), like this:

MacBook-Pro:~ jk$ █

If you log in as the root user (see Perform Actions as the Root User), the prompt ends instead in a # character:

bash-3.2# █

Other shells have different default characters. For example, in the zsh shell, the prompt normally ends with a %. As a result, when you’re reading articles and Web sites listing commands you might enter in Terminal, you might run across examples like these:

$ open -e file1

# chown www file1

% top

The $, #, or % at the beginning merely signifies that what follows is a command to be typed and, in the case of #, that it’s supposed to be typed by the root user. You wouldn’t actually type $, #, or %.

I don’t use that convention in this book; whatever you need to type on the command line simply appears in a special font, usually on a line by itself. I find those extra characters distracting.

In any case, you can easily change the prompt so that it shows something else entirely. If you want your prompt to look like this…

Joe rocks +> █

…you can make that happen. See Change Your Prompt for details.