Up Next Previous

Completion and listing (+)

The shell is often able to complete words when given a unique abbreviation. Type part of a word (for example `ls /usr/lost') and hit the tab key to run the complete-word editor command. The shell completes the filename `/usr/lost' to `/usr/lost+found/', replacing the incomplete word with the complete word in the input buffer. (Note the terminal `/'; completion adds a `/' to the end of completed directories and a space to the end of other completed words, to speed typing and provide a visual indicator of successful completion. The addsuffix shell variable can be unset to prevent this.) If no match is found (perhaps `/usr/lost+found' doesn't exist), the terminal bell rings. If the word is already complete (perhaps there is a `/usr/lost' on your system, or perhaps you were thinking too far ahead and typed the whole thing) a `/' or space is added to the end if it isn't already there.

Completion works anywhere in the line, not just at the end; completed text pushes the rest of the line to the right. Completion in the middle of a word often results in leftover characters to the right of the cursor which need to be deleted.

Commands and variables can be completed in much the same way. For example, typing `em[tab]' would complete `em' to `emacs' if emacs were the only command on your system beginning with `em'. Completion can find a command in any directory in path or if given a full pathname. Typing `echo $ar[tab]' would complete `$ar' to `$argv' if no other variable began with `ar'.

The shell parses the input buffer to determine whether the word you want to complete should be completed as a filename, command or variable. The first word in the buffer and the first word following `;', `|', `|&', `&&' or `||' is considered to be a command. A word beginning with `$' is considered to be a variable. Anything else is a filename. An empty line is `completed' as a filename.

You can list the possible completions of a word at any time by typing `^D' to run the delete-char-or-list-or-eof editor command. The shell lists the possible completions using the ls-F builtin (q.v.) and reprints the prompt and unfinished command line, for example:

> ls /usr/l[^D]
lbin/ lib/ local/ lost+found/
> ls /usr/l

If the autolist shell variable is set, the shell lists the remaining choices (if any) whenever completion fails:

> set autolist
> nm /usr/lib/libt[tab]
libtermcap.a@ libtermlib.a@
> nm /usr/lib/libterm

If autolist is set to `ambiguous', choices are listed only when completion fails and adds no new characters to the word being completed.

A filename to be completed can contain variables, your own or others' home directories abbreviated with `~' (see Filename substitution) and directory stack entries abbreviated with `=' (see Directory stack substitution). For example,

> ls ~k[^D]
kahn kas kellogg
> ls ~ke[tab]
> ls ~kellogg/

or

> set local = /usr/local
> ls $lo[tab]
> ls $local/[^D]
bin/ etc/ lib/ man/ src/
> ls $local/

Note that variables can also be expanded explicitly with the expand-variables editor command.

delete-char-or-list-or-eof only lists at the end of the line; in the middle of a line it deletes the character under the cursor and on an empty line it logs one out or, if ignoreeof is set, does nothing. `M-^D', bound to the editor command list-choices, lists completion possibilities anywhere on a line, and list-choices (or any one of the related editor commands which do or don't delete, list and/or log out, listed under delete-char-or-list-or-eof) can be bound to `^D' with the bindkey builtin command if so desired.

The complete-word-fwd and complete-word-back editor commands (not bound to any keys by default) can be used to cycle up and down through the list of possible completions, replacing the current word with the next or previous word in the list.

The shell variable fignore can be set to a list of suffixes to be ignored by completion. Consider the following:

> ls
Makefile condiments.h~ main.o side.c
README main.c meal side.o
condiments.h main.c~
> set fignore = (.o \~)
> emacs ma[^D]
main.c main.c~ main.o
> emacs ma[tab]
> emacs main.c

`main.c~' and `main.o' are ignored by completion (but not listing), because they end in suffixes in fignore. Note that a `\' was needed in front of `~' to prevent it from being expanded to home as described under Filename substitution. fignore is ignored if only one completion is possible.

If the complete shell variable is set to `enhance', completion 1) ignores case and 2) considers periods, hyphens and underscores (`.', `-' and `_') to be word separators and hyphens and underscores to be equivalent. If you had the following files

comp.lang.c comp.lang.perl comp.std.c++
comp.lang.c++ comp.std.c

and typed `mail -f c.l.c[tab]', it would be completed to `mail -f comp.lang.c', and ^D would list `comp.lang.c' and `comp.lang.c++'. `mail -f c..c++[^D]' would list `comp.lang.c++' and `comp.std.c++'. Typing `rm a--file[^D]' in the following directory

A_silly_file a-hyphenated-file another_silly_file

would list all three files, because case is ignored and hyphens and underscores are equivalent. Periods, however, are not equivalent to hyphens or underscores.

Completion and listing are affected by several other shell variables: recexact can be set to complete on the shortest possible unique match, even if more typing might result in a longer match:

> ls
fodder foo food foonly
> set recexact
> rm fo[tab]

just beeps, because `fo' could expand to `fod' or `foo', but if we type another `o',

> rm foo[tab]
> rm foo

the completion completes on `foo', even though `food' and `foonly' also match. autoexpand can be set to run the expand-history editor command before each completion attempt, autocorrect can be set to spelling-correct the word to be completed (see Spelling correction) before each completion attempt and correct can be set to complete commands automatically after one hits `return'. matchbeep can be set to make completion beep or not beep in a variety of situations, and nobeep can be set to never beep at all. nostat can be set to a list of directories and/or patterns which match directories to prevent the completion mechanism from stat(2)ing those directories. listmax and listmaxrows can be set to limit the number of items and rows (respectively) that are listed without asking first. recognize_only_executables can be set to make the shell list only executables when listing commands, but it is quite slow.

Finally, the complete builtin command can be used to tell the shell how to complete words other than filenames, commands and variables. Completion and listing do not work on glob-patterns (see Filename substitution), but the list-glob and expand-glob editor commands perform equivalent functions for glob-patterns.

Up Next Previous