Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

 

 

Using the Escape Key to Your Advantage

31

TABLE 5-1: GETTING THE ITEM YOU WANT WITH AUTOCOMPLETE

 

 

 

 

 

 

If the Partial Text Begins With . . .

bash Looks For . . .

Example

 

 

 

 

 

$

A matching environment

If you type cd $HO and then press Tab, bash

 

 

variable

translates that to cd $HOME.

 

~ (tilde)

A matching user name

Typing cd ~fre and pressing Tab translates to

 

 

 

cd ~freddie/.

 

@

A matching host name

mail freddie@bas followed by a tab translates to

 

 

 

freddie@bastille.

 

No special symbol

A command name completion

Type more /etc/pass and then press Tab, and bash

 

and, finally, a filename

completes your command as more /etc/passwd.

 

 

 

 

 

make the choice unambiguous, and bash will complete the command name.

4. After bash completes the command name, press Enter, and the command is executed.

If, at any time, you press the Tab key and nothing happens (or you just hear a beep), bash either found no completions or a bunch of completions. (In this context, bunch is a technical term that means some number greater than one.) If you press Tab a second time, you see a list of all possible completions (if any exist).

Using the Escape Key to Your Advantage

The Tab key completes environment variables, user names, host names, command names, and filenames. You can fine-tune bash completions with the Esc key. Use the Esc key in combination with other keys to limit the type of completion that bash attempts, or to view or insert several completions at once. Table 5-2 has all the details.

The Esc-key options are very powerful, but we’ve never bothered to memorize them all. Your number-one timesaving friend is the Tab key. We use it all day long.

bash can complete any filename pattern, not just a prefix. If you have a few tarballs (that is, files whose

names end in .tgz) in your current directory, add all of them to the command line like this:

[freddie@bastille] ls -l *.tgz <Esc-*>

When you press Esc-*, bash replaces the *.tgz part with the names of all files matching that pattern.

Environment variables that affect filename completion

Set the following environment variables to screen out files you don’t want to include in the completion list:

FIGNORE: This is a colon-separated list of file suffixes to ignore during filename completion. For example, compilers often produce filenames that end in .o. If you want the filename completion mechanism to ignore those files, set FIGNORE to .o, like this:

$ export FIGNORE=.o

HOSTFILE: Use this environment variable to tell the completion mechanism which hosts to consider when completing a host name. If you don’t set HOSTFILE, bash searches the /etc/hosts file. You can use HOSTFILE to limit host name completion to only those hosts that you use frequently. If you’ve created a file named ~/myhosts that contains the names of the hosts that you frequent, set HOSTFILE like this:

$ export HOSTFILE=~/myhosts

If you want your environment variables in place every time you log in, see Technique 8, in which we show you how to modify your login and logout scripts.

32 Technique 5: Getting There Quick with Dynamic Shortcuts

TABLE 5-2: USING ESCAPE FOR COMPLETIONS

What to Press What It Does

Timesaving Bonus Info

Esc-? Displays all possible completions — command names, filenames, and user names — and presents them in table form for you to read through and complete the command.

Esc-? works very much like the Tab key except that it doesn’t actually complete a word; it just shows you the possible completions. If you want to see a (very long) list of all the commands in your search path, press Esc-? in a blank command line.

Esc-*

Inserts all possible completions into your command.

Esc-/

Completes the filename to the left of the cursor.

This is helpful if you have few possible completions.

Esc-/ attempts filename completion only. That’s handy when you know you want a filename — you won’t get a host name or user name by accident.

Esc-~

Completes the user name to the left of the cursor.

Complete user names only — don’t try the other completion types.

Esc-$

Completes the variable name to the left of the cursor.

Esc-@

Completes the host name to the left of

 

the cursor.

Complete variable names only — don’t try the other completion types.

Complete host names only — don’t try the other completion types. This option is useful when you need to type a host name, but you don’t have a @ in the command, for example, ssh bastille.

Esc-! Completes the command name to the left of the cursor.

Complete command names only — don’t try the other completion types.

Customizing Completion for Maximum Speed

Suppose that you take care of a network of computers and you find yourself logging into remote hosts by using ssh. To ssh to host bastille, you might type in ssh b and then press Tab thinking that bash will fill in the rest of the host name (bastille) for you. bash doesn’t know that ssh is always followed by a host name, so instead of doing what you want, bash goes through its normal search routine trying to find a matching filename. To tell bash to complete hostnames for ssh, use the following command:

[freddie@bastille] complete –A hostname ssh

The -A hostname part tells bash that you want to complete host names (from the /etc/hosts file or from $HOSTFILE if defined). The ssh part tells bash which command you want to customize. You can customize several (probably related) commands at the same time, for example:

[freddie@bastille] complete –A hostname ssh sftp rsh ping

This command tells bash to complete host names for ssh, sftp, rsh, and ping. Of course, you can tell bash to use other completion types, too:

[freddie@bastille] complete –A username usermod passwd

Customizing Completion for Maximum Speed

33

This command tells bash to complete user names after the usermod and passwd commands. The most useful completion actions are listed in Table 5-3.

TABLE 5-3: USEFUL COMPLETION ACTIONS

Use This Action

To Do This

-A command

Complete command names (useful for the

 

which command).

-A directory

Complete directory names (perfect for

 

cd).

-A file

Complete filenames.

-A hostname

Complete host names.

-A user

Complete user names.

 

 

bash supports many completion actions in addition to the ones listed in Table 5-3. See man bash for more options.

You can also customize completion for a command by creating filters. If you use OpenOffice.org frequently, you may want to customize completion for OOWriter and OOCalc:

[freddie@bastille] complete –G “*.sxw” oowriter

[freddie@bastille] complete –G “*.sxc” oocalc

Now when you type oowriter and press Tab, bash only completes filenames that end in .sxw (the OOWriter file format). The second command tells bash to complete OOCalc spreadsheets when you run oocalc.

Don’t forget to save your customizations to one of the bash startup files. See Technique 8 if you’re not sure which file to use.

6 Using cd Shortcuts for Rapid Transit

Technique

Save Time By

Using bash (rather than a graphical interface) for file management

Getting around your disk quickly

Defining search paths to take you places fast

Remembering where you’ve been with pushd and popd

You can use Linux for ages without venturing near the terminal window. With all the graphical programs available, you can do virtually anything without ever having to go near the command line. The

downside of heavy dependence on a graphical interface is that you lose speed — few graphical programs provide good looks and high power in the same package.

Getting around quickly is a matter of knowing the fastest route, whether you’re using the command line or a browser. bash (the program in charge of the command line) knows this, and helps out with a bunch of handy ways to jump to the locations you need when you use the command line.

Backtracking at the command line can be a timesaver, too. In this technique, we show you how to use pushd and popd to make a retraceable path. You’ll be moving back and forth through your directories in no time.

We also introduce you to a handy environment variable — CDPATH — that you can use to make directory changes quickly without searching for the correct pathnames. The CDPATH variable makes the command line friendlier and faster.

Can’t find your way out of a paper bag? After reading this technique, you’ll know not only where you are, but also where you’ve been and the quickest way to get where you’re going!

Using cd and ls to Navigate through bash

The cd command is the mode of travel through the terminal window. With cd, you can go anywhere fast:

To return to your home directory, type cd and press Enter.

To go to a specific directory, type cd, a space, and the directory name; then press Enter.

Соседние файлы в предмете Операционные системы