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:
If the autolist shell variable is set, the shell lists the remaining choices (if any) whenever completion fails:
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,
or
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:
`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
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
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:
just beeps, because `fo' could expand to `fod' or `foo', but if we type another `o',
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.