[Chapter 9] 9.8 Filename Completion: Faster Filename Typing
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:35:01Z

#

| Chapter 9 Saving Time on the Command Line |

## 9.8 Filename Completion: Faster Filename Typing

If you hate typing long filenames, you should know about the shells' "filename completion" feature. It varies shell-to-shell, so we'll give an overview here. Check the manpage or the shell's Nutshell Handbook for details.

You can type the initial part of a filename, and then press the TAB key (in the C shell, use ESC). If the shell can figure out the complete filename from the part that you've typed, it will fill in the rest of the name. If not, it will fill in as much of the name as is unambiguous, and then let you type some more. (To enable this in most C shells, set the variable filec (6.9) or complete. Older C shells don't have this feature.) For example:

>     $ ls
>     alpha.c    alpha.o   beta.c
>     $ cc b [TAB]
>     $ cc beta.cShell fills in the filename automatically

(With tcsh and csh, your terminal will beep if more than one file matches the name you've typed. If all this beeping drives you crazy, you can set the nobeep shell variable; that will turn it off.) In this case, only one filename begins with b, so the shell can fill in the entire name.

If you type part of a filename and then type CTRL-d (in bash, type TAB twice), the shell lists all the files that match whatever you've typed. It then redisplays your command line and lets you continue typing. For example:

>     % cc a[CTRL-d]
>     alpha.c    alpha.o
>     % cc alpha.

Two files begin with the letter "a"; the shell lists them. It then redisplays the cc command, letting you finish the filename.

> NOTE: Also, be forewarned that filename completion is a hack and doesn't always work correctly. For example: if you're using SunOS, you can't use filename completion within a "command tool" (SunOS 4.1). Don't try to mix filename completion with wildcards; it won't work. We can't go into detail about these rough edges, but if you're aware that they exist, you won't have trouble.

Article 14.9 shows an interesting shortcut to filename completion: cding to a directory by typing its "initials."

- ML, JP

9.7 String Editing in ksh and bash |  | 9.9 Don't Match Useless Files in Filename Completion