Go to the first, previous, next, last section, table of contents.


Completion System

Description

This describes the shell code for the new completion system. It consists of various shell functions; those beginning `comp' are to be called directly by the user, while those beginning `_' are called by the completion code. The shell functions of the second set which implement completion behaviour and which may be bound to keystrokes, are referred to as `widgets'.

Note that with the function-based completions described here, it is also possible to use the compmatchers special array to specify global matching control, such as case-insensitivity (`abc' will complete to a string beginning `ABC'), or wildcard behaviour on certain anchors (`a-d' will complete to abc-def as if there were a `*' after the `a'). See section Matching Control for further details.

Initialization

The function compinstall can be run by a user to set up the completion system for use, which also provides options for more advanced usage. However, if the system was installed completely, it should be enough to call the shell function compinit from your initialization file; see the next section.

Usually, compinstall will insert code into .zshrc, although if that is not writable it will save it in another file and tell you that file's location. Note that it is up to you to make sure that the lines added to .zshrc are actually run; you may, for example, need to move them to an earlier place in the file if .zshrc usually returns early. So long as you keep them all together (including the comment lines at the start and finish), you can rerun compinstall and it will correctly locate and modify these lines. Note, however, that any code you add to this section by hand is likely to be lost if you rerun compinstall. The new code will take effect next time you start the shell, or run .zshrc by hand.

To run it, you will need to make sure it is in a directory mentioned in your $fpath parameter, and that it is autoloaded (`autoload -U compinstall' is recommended). It will ask you various questions about how you would like completion set up. It is in two parts; the basic part locates the completion files and decides where to put your personal dumpfile, used to speed up initialization after the first time. After that, you will be asked if you wish to go on to the advanced set-up; if you answer n, you can rerun compinstall later without having to re-enter any of the basic settings.

You can abort the installation any time you are being prompted for information, and your .zshrc will not be altered at all.

After initialization all the builtin completion widgets such as expand-or-complete will be redefined to use the new completion system. Should you need to, you can still bind keys to the old functions by putting a `.' in front, e.g. `.expand-or-complete'.

Use of compinit

This section describes the use of compinit to initialize completion for the current session when run directly by the user; if you have run compinstall it will be called automatically from your .zshrc.

To initialize the system, the function compinit should be in a directory mentioned in the $fpath variable, and should be autoloaded (`autoload -U compinit' is recommended). When run, it will define a few utility functions, arrange for all the necessary shell functions to be autoloaded, and will then re-bind all keys that do completion to use the new system.

To speed up the running of compinit, it can be made to produce a dumped configuration which will be read in on future invocations; this is the default, although it can be turned off by calling compinit with the option -D. The dumped file is .zcompdump in the same directory as the startup files (i.e. $ZDOTDIR or $HOME); alternatively, an explicit file name can be given by `compinit -d dumpfile'. On the next call to compinit, the dumped file will be read instead of a full initialization.

If the number of completion files changes, compinit will recognise this and produce a new dump file. However, if the name of a function or the arguments in the first line of a #compdef function (as described below) change, it is easiest to delete the dump file by hand so that the next time compinit will re-create it.

The dumping is actually done by another function, compdump, but you will only need to run this yourself if you change the configuration (e.g. using compdef) and then want to dump the new one. The name of the old dumped file will be remembered for this purpose.

If the parameter _compdir is set, compinit uses it as a directory where completion functions can be found; this is only necessary if they are not already in the function search path.

Autoloaded files

The convention for autoloaded functions used in completion is that they start with an underscore; as already mentioned, the fpath/FPATH parameter must contain the directory in which they are stored. If zsh was properly installed on your system, then fpath/FPATH automatically contains the required directories.

For incomplete installations, if compinit does not find enough files beginning with an underscore (fewer than twenty) in the search path, it will try to find more by adding the directory _compdir to the search path; if you have run compinstall, this will be set automatically. Furthermore, if the directory in question ends in the path segment Core, or has a subdirectory named Core, compinit will add all subdirectories of the directory where Core is to the path: this allows the functions to be in the same format as in the zsh source distribution.

When compinit is run, it searches all such files accessible via fpath/FPATH and reads the first line of each of them. This line should contain one of the tags described below. Files whose first line does not start with one of these tags are not considered to be part of the completion system and will not be treated specially.

The tags are:

#compdef names...
The file will be made autoloadable and the function defined in it will be called when completing names, each of which is either the name of a command whose arguments are to be completed or one of a number of special contexts in the form -context- described below for the _complete function.
#compdef -p pattern
The file will be made autoloadable and the function defined in it will be called when completing for a command whose name matches the given pattern (a standard globbing pattern). Note that only one pattern may be given.
#compdef -P pattern
Like the previous one, but the function will be called only if no completion function for the command on the line could be found.
#compdef -k style key-sequences...
This can be used to bind special completion functions to the key-sequences. It creates a widget behaving like the builtin widget style, which must be one of those that perform completion, namely complete-word, delete-char-or-list, expand-or-complete, expand-or-complete-prefix, list-choices, menu-complete, menu-expand-or-complete, or reverse-menu-complete. If the complist module is loaded (see section The zsh/complist Module), the menu-select widget can be used, too. Note that the bindings will not be used if the key is already bound (that is, is bound to something other than undefined-key). The widget is then bound to all the key-sequences given, if any: when one of the key-sequences is typed, the function in the file will be invoked to generate the matches. The widget created has the same name as the file and can also be bound to other keys using bindkey as usual.
#compdef -K widget-name style key-sequences ...
This is similar to -k, with the same style and key-sequences arguments arguments, preceeded by a string giving the name of a widget. In this case only one key-sequences argument may be given, but the entire set of three arguments may be repeated with a different set of arguments. In particular, the widget-name must be distinct in each set. It should begin with `_', else one will be added, and should not clash with the name of any existing widget: names based on the name of the function are most useful. For example,
#compdef -K _foo_complete complete-word "^X^C" \ 
  _foo_list list-choices "^X^D"
(all on one line) defines a widget _foo_complete for completion, bound to `^X^C', and a widget _foo_list for listing, bound to `^X^D'.
#autoload
This is used for files defining utility function that are not to be called directly as completion functions but should be loaded automatically when invoked. Typically they are to be called from within one of the completion functions.

Note that the # is part of the tag name and no white space is allowed after it. The #compdef tags use the compdef function described below; the main difference is that the name of the function is supplied implicitly.

Functions

The compinit file defines the following functions, which may also be called directly by the user.

compdef [ -an ] function names...
compdef -d names...
compdef -p [ -a ] function pattern
compdef -P [ -a ] function pattern
compdef -k [ -an ] function style key-sequences...
compdef -K [ -an ] function name style key-sequences ...
The first form tells the completion system to call the given function when completing for the contexts or commands whose names are given: this is like the #compdef tag. If the -n option is given, any existing completion behaviour for particular contexts or commands will not be altered. These definitions can be deleted by giving the -d option as in the second form. The form with -p is similar to the first, but function will be called for all commands whose name matches the pattern; this is like the #compdef -p function tag. The form with -P is like the third, but the function will be called only if no function for the command itself was found or if one was found and it set the _compskip parameter to a value not containing the substring patterns. The form with -k defines a widget with the same name as the function which will be called for each of the key-sequences; this is like the #compdef -k tag. The function should generate the completions needed and will otherwise behave like the builtin widget whose name is given as the style argument. The widgets usable for this are: complete-word, delete-char-or-list, expand-or-complete, expand-or-complete-prefix, list-choices, menu-complete, menu-expand-or-complete, and reverse-menu-complete, as well as menu-select if the zsh/complist module is loaded. The option -n prevents the key being bound if it is already to bound to something other than undefined-key. The form with -K is similar and defines multiple widgets based on the same function, each of which requires the set of three arguments name, style and key-sequences, where the latter two are as for -k and the first must be a unique widget name beginning with an underscore. In each of the forms supporting it the -a option makes the function autoloadable (exactly equivalent to autoload -U function).

Completion System Configuration

This section gives a short overview of how the completion system works, and then more detail on how users can configure how and when matches are generated.

Overview

When completion is attempted somewhere on a command line the completion system first tries to find out the context where completion was tried. The context depends on such things as the name of the command when completing an argument, and possibily also the name of an option when completing an argument to that option.

The completion system represents contexts as hierarchical name s with components separated by colons. For example, take the context `:completion:complete::dvips::-o-1'. The :completion at the beginning just says that this context is used in the completion system and the :complete after it is the `completer', which is in overall control of how completion is to be performed; `complete' is the basic one for ordinary completion, but completers may perform various related tasks such as correction, or modify the behaviour of a later completer (see section Control Functions for more information). Strictly, the completer is `_complete', but the underscore is omitted from the context; this is also true of `correct', `approximate', etc. The ::dvips: shows that we are completing arguments for the dvips command. The doubled colon will appear only before and after the name of the command, but note that the second colon after the command name is only added when there is at least one more component (otherwise the whole name ends in a colon, e.g. `...dvips:'). Finally, the string -o-1 says that we are completing the first argument of the option `-o' to the command. Note that the existence of a context like this does not necessarily mean it is handled specially by the completion system; this is determined by trying to match the context as specifically as possible, as described below.

In many of the possible contexts the completion system can generate matches, often multiple types of matches. These types are represented as simple names called `tags'. The completion system will decide internally what sort of tags are allowed; a list of the standard possibilities is given below. To determine in which order the tags are to be used by the completion function, the `tag-order' style for the appropriate context may be set, as described in the list of standard styles below. Only those types of matches whose tags were selected by this style will be produced, and in the order given.

The _complete_help bindable command described in section Bindable Commands can be invoked to find out the context and tag names used at a particular point in completion. It shows a list of context names and the tag names used in those contexts if completion were tried at the current cursor position. Hence one can easily find out all the information needed to change the behaviour of the tag-order style for a particular context.

Completion behaviour can be modified by various other `styles' defined with the zstyle builtin command (see section The zsh/zutil Module). The full context used in looking up styles is the prefix `:completion' followed by the context as described above, followed by another colon and the name of the tag currently being tried for completion.

Styles determine such things as how the matches are generated; some of them correspond to shell options (for example, the use of menu completion), but styles provide more specific control. They can have any number of strings as their value. Looking up the value of a style therefore consists of two things: the context, which may be matched as a pattern, and the name of the style itself, which must be given exactly.

For example, many completion functions can generate matches in a simple and a verbose form and use the verbose style to decide which form should be used. To make all such functions always use the verbose form one can simply call

zstyle ':completion:*' verbose yes

in one of the startup files like .zshrc. This definition simply means that the verbose style has yes as its value in every context inside the completion system. If the pattern were `*', it would mean that the verbose style had this value anywhere the style mechanism is used.

As a more specific example, the completion function for the kill builtin command uses the verbose style to decide if jobs and processes are listed only as job numbers and process identifiers or if they are listed with the full job texts and the command lines of the processes (the latter is achieved by calling the ps command). To make this builtin list the matches only as numbers one could call:

zstyle ':completion:*::kill:*' verbose no

Furhtermore, if one wanted to see the command lines for processes but not the job texts one could use the fact that the tag name is appended to the context name when styles are looked up. As the function for the kill builtin command uses the tags jobs and processes, we have:

zstyle ':completion:*::kill:*:jobs' verbose no

Note that the order in which styles are defined does not matter; the style mechanism uses the most specific possible match for a particular style to determine the set of values. More precisely, strings are preferred over patterns (for example, `:completion:complete:foo' is more specific than `:completion:complete:*'), and longer patterns are preferred over shorter patterns.

As for tags, completion functions can use any number of styles, so there can't be a complete list. However, the following two sections list those tags and styles that are used in many places of the completion system.

Standard Tags

Here are the tags currently used by the completion system. Note that some of these tags are not actually used while generating matches, but are only used by some completion functions when looking up styles.

accounts
used to look up the users-hosts style
all-files
for the names of all files
all-expansions
used by the _expand completer when adding the string containing all expansions
arguments
when an argument of a command may be completed
arrays
for names of array parameters
association-keys
for keys of associative arrays (e.g. when completing inside a subscript of such a parameter)
bookmarks
when completing bookmarks (e.g. for URLs and the zftp function suite)
builtins
for names of builtin commands
characters
used for commands like stty when completing characters; also used when completing character classes after a opening bracket
colors
for color names
commands
for names of external commands and names of sub-commands (used by some commands like cvs)
corrections
used by the _approximate and _correct completers for the possible corrections
cursors
for cursor names used by X programs
cvs
used only to look up the value of the disable-stat style
default
used to look up default values for various styles that may also be set for tags that are used when generating matches
descriptions
used when looking up the value of the format style for descriptions
devices
for names of device special files
directories
for names of directories
directory-stack
for entries in the directory stack
displays
for X display names
domains
for network domains
expansions
used by the _expand completer for possible expansions
extensions
for X server extensions
files
used by completion functions that can complete some kind of filenames and different types of matches
fonts
used for X font names
functions
names of functions (shell functions or other kinds of functions for some commands)
globbed-files
for names of files matching the glob pattern used by completion functions that expect a certain type of file
groups
used when completing names of user groups
history-words
for words from the history
hosts
for hostnames
indexes
used for array indexes
jobs
used for jobs
keymaps
for names of zsh keymaps
keysyms
for names of X keysyms
local-directories
for names of directories in the current working directory when completing for the cd builtin command
libraries
for names of system libraries
limits
for system limits
manuals
for names of manual pages
maps
for map names (e.g. YP maps)
messages
used to look up the format style for messages
modifiers
for names of X modifiers
modules
for modules (e.g. zsh modules)
my-accounts
used to look up the users-hosts style
named-directories
for named directories (you wouldn't have guessed that, would you?)
names
for all kinds of names
nicknames
for nicknames of YP maps
options
for command options
original
used by the _approximate, _correct and _expand completers when adding the original string
other-accounts
used to look up the users-hosts style
packages
for packages (e.g. rpm packages)
parameters
for names of parameters
path-directories
for names of directories in directories from the cdpath array when completing for the cd builtin command
paths
used to look up the values of the expand, cursor and special-dirs styles
pods
for perl pods
ports
for communication ports
prefixes
for prefixes (like those of an URL)
printers
for printer names
processes
for process identifiers
ps
used to look up the arguments and list-arguments styles
sequences
for sequences (e.g. mh sequences)
sessions
for sessions in the zftp function suite
signals
for signal names
strings
for strings (e.g. the replacement strings for the cd builtin command)
tags
for tags (e.g. rpm tags)
targets
for makefile targets
types
for types of whatever (e.g. adress types for the xhost command)
urls
used to look up the path and local styles when completing URLs
users
for usernames
values
when completing a value out of a set of values (or a list of such values)
warnings
used to look up the format style for warnings
widgets
for zsh widget names
windows
for IDs of X windows
zsh-options
for shell options

Standard Styles

Here are the names of the styles used by the completion system. Note that the values of several of these styles represent boolean values. In all these cases any of the strings `true', `on', `yes', and `1' can be used for the truth value `true' and the strings `false', `off', `no', and `0' are interpreted as `false'. The behavior for any other value is undefined unless the description for the particular style mentions other possible values.

accept-exact
This is tested for the default tag and the tags used when generating matches. If it is set to `true' for at least one match which is the same as the string on the line, this match will immediately be accepted.
arguments
The value of this style is given to the ps command by functions that call it when generating process identifiers as matches.
auto-description
If set, this style's value will be used as the description for options which are not described by the completion functions, but that have exactly one argument. The sequence `%d' in the value will be replaced by the description for this argument. Depending on personal preferences, it may be useful to set this style to something like `specify: %d'. Note that this may not work for some commands.
completer
The strings given as the value of this style give the names of the completer functions to use. The available completer functions are described in section Control Functions. Note that the widget functions from the distribution that call the completion code (namely, the incremental-complete-word and the predict-on widgets) set up their top-level context name before calling completion. This allows one to define different sets of completer functions for normal completion and for these widgets. For example, to use completion, approximation and correction for normal completion, completion and correction for incremental completion and only completion for prediction one could use:
zstyle ':completion:*' completer _complete _correct _approximate
zstyle ':completion:incremental' completer _complete _correct
zstyle ':completion:predict' completer _complete
completions
This style is used by the _expand completer function. If this is set to an non-empty string it should be an expression usable inside a `$((...))' arithmetical expression. The completer function evaluates this expression and if the result is `1', no expansions will be generated, but instead the completions will be generated as normal and all of them will be inserted into the command line.
condition
This style is used by the _list completer function. If it is not set or set to the empty string, the insertion of matches will be delayed unconditionally. If it is set, the value should be an expression usable inside a `$((...))' arithmetical expression. In this case, delaying will be done if the expression evaluates to `1'. For example, with
zstyle ':completion:list' condition 'NUMERIC != 1'
delaying will be done only if given an explicit numeric argument other than `1'.
cursor
This is used together with the paths tag by the function generating filenames as matches to find out if the cursor should be left after the first ambiguous pathname component even when menucompletion is used.
disable-stat
This is used with the cvs tag by the function completing for the cvs command to decide if the zsh/stat module should be used to generate only names of modified files in the appropriate places.
domains
If set, gives the names of network domains that should be completed. If this is not set by the user domain names mentioned in /etc/resolv.conf will be used.
expand
Like cursor, this style is used with the paths tag. If its value contains the string prefix, the partially typed path from the line will be expanded as far as possible even if trailing pathname components can not be completed. If it contains the string suffix and normal (non-menu-) completion is used, matching names for components after the first ambiguous one will be added, too. This means that the resulting string is the longest unambiguous string possible, but if menucompletion is started on the list of matches generated this way (e.g. due to the option AUTO_MENU being set), this will also cycle through the names of the files in pathname components after the first ambiguous one.
file-patterns
The completion system uses two functions to complete filenames, _files and _path_files, with one of them calling the other, but this second one is sometimes also called directly. Depending on how it is called, the first one uses the tags globbed-files, directories and all-files. The second one, when called directly, uses the tag files. Using this style one can specify which filenames should be completed in certain contexts. It is tested by _files and, if called directly, _path_files with the tags mentioned above. If it is set for these tags, the value is taken as a list of glob-patterns that should be used to select filenames when completing for the tag. Note that with _files, calling completion function may specify that all files are to be completed. Normally this would make _files use only the all-files tag, but if this style is set for any of the other two tags (globbed-files and directories), these tags will be used, too, in the normal order given above (unless the user specified another order to be used with the tag-order style). For example, to make the completion system first try only filenames matching the pattern *.o for the rm command, one would use:
zstyle ':completion:*::rm*:globbed-files' file-patterns '*.o'
With this, using only filenames ending in .o will be the first choice and other filenames will only be used if what is on the line matches none of the .o files (or if there are none). Note also that during the execution of completion functions, the EXTENDED_GLOB option is in effect, so the characters `#', `~' and `^' have special meanings in the patterns.
format
If this is set for the descriptions tag, its value is used as a string to display above matches in completion lists. The sequence `%d' in this string will be replaced with a short description of what these matches are. This string may also contain the sequences to specify output attributes, such as `%b' and `%s'. For the same purpose, this style is also tested with the tags used when matches are generated before it is tested for the descriptions tag. This gives the possibility to define different format strings for different types of matches. Note also that some completer functions define additional `%'-sequences. These are described for the completer function that make use of them. For the messages tag, this defines a string used by some completion functions to display messages. Here, the `%d' is replaced with the message given by the completion function. Finally, for the warnings tag, it is printed when no matches could be generated at all. In this case the `%d' is replaced with the descriptions for the matches that were expected. Here and in all other cases where the completion system uses `%' sequences, the `%' may be followed by field with specifications as described for the zformat builtin command from the zutil module, see section The zsh/zutil Module.
glob
Like completions, this is used by the _expand completer. The value is used like the one for completions and if it evaluates to `1', globbing will be attempted on the words resulting from substitution (see the substitute style) or the original string from the line.
group-name
The completion system can put different types of matches in different groups which are then displayed separately in the list of possible completions. This style can be use to give the names for these groups for particular tags. For example, in command position the completion system generates names of builtin and external commands, names of aliases and shell functions and reserved words as possible completions. To have the external commands and shell functions listed separately, one can set:
zstyle ':completion:*:-command-:commands' group-name commands
zstyle ':completion:*:-command-:functions' group-name functions
This also means that if the same name is used for different types of matches, then those matches will be displayed together in the same group. If the name given is the empty string, then the name of the tag for the matches will be used as the name of the group. So, to have all different types of matches displayed separately, one can just set:
zstyle ':completion:*' group-name ''
All matches for which no group name is defined will be put in a group named -default-.
group-order
This style is to be used together with the group-name style. Once different types of matches are put into different groups, this style can be used to define in which order these groups should appear in the list. The strings in the value are taken as group names and the named groups will be shown in the order in which their names appear in the value. All groups whose names are not given in the value of this style will appear in the order defined by the function generating the matches. For example, to have names of builtin commands, shell functions and external commands appear in this order when completing in command position one would set:
zstyle ':completion:*:-command-' group-order builtins functions commands
groups
A style holding the names of the groups that should be completed. If this is not set by the user, the group names from the YP database or the file `/etc/group' will be used.
hidden
If this is set to one of the `true' values, the matches for the tags for which this is set will not appear in the list; only the description for the matches as set with the format style will be shown. If this is set to all, not even the description will be displayed. Note that the matches will still be completed, they are just not shown in the list. To avoid having matches considered as possible completions at all the tag-order style can be modified as described below.
hosts
A style holding the names of hosts that should be completed. If this is not set by the user the hostnames in `/etc/hosts' will be used.
hosts-ports
This style is used by commands that need or accept hostnames and ports. The strings in the value should be of the form `host:port'. These hostnames and ports are completed depending on the information already on the line, so that if, for example, the hostname is already typed, only those ports will be completed for which pairs with the hostname from the line exist.
hosts-ports-users
Like hosts-ports but used for commands like telnet and containing strings of the form `host:port:user'.
ignore-parents
When completing files it is possible to make names of directories already mentioned on the line or the current working directory be ignored. The style is tested for the files tag and if its value contains the string parent, then the name of any directory whose path is already contained in the word on the line is ignored. For example, when completing after foo/../, the directory foo will not be considered a valid completion. If the style contains the string pwd, then the name of the current working directory will not be completed, so that, for example, completion after ../ will not use the name of the current directory. If the style contains the string .. both tests will only be performed if the word on the line contains the substring ../ and if the value contains the string directory, then the tests will only be performed if only names of directories are completed. Note that names of directories ignored because of one of the tests will be placed in the alternate set of completions so that they will be completed if there are no other possible completions.
ignored-patterns
This style is used with the tags used when adding matches and gives a number of patterns. All matches that are matched by any of these patterns will be ignored as long as there are other matches not matched by any of the patterns. It is a more configurable version of the shell parameter $fignore. Note that during the execution of completion functions, the EXTENDED_GLOB option is in effect, so the characters `#', `~' and `^' have special meanings in the patterns.
insert-unambiguous
This is used by the _match and _approximate completer functions. If it is set to `true', the completer will start menu completion only if no unambiguous string could be generated that is at least as long as the original string from the line.
last-prompt
This is used to determine if the completion code should try to put the cursor back onto the previous command line after showing a completion listing (as for the ALWAYS_LAST_PROMPT option). Like several other styles it is tested for the default tag and all tags used when generating matches. The cursor will be moved back to the previous line if this style is `true' for all types of matches added. Note also that this is independent of the numeric argument -- unlike the ALWAYS_LAST_PROMPT option.
list
This is used by the _oldlist completer (context `:completion:oldlist'), the _history_complete_word bindable command (context `:completion:history-words') and by the incremental-complete-word widget (context `:completion:incremental). For _oldlist, if this is set to always, then standard widgets which perform listing will retain the current list of matches, however they were generated. If it is set to never, this will not be done (the behaviour without the _oldlist completer). If it is unset, or any other value, then the existing list of completions will be displayed if it is not already; otherwise, the standard completion list will be generated: this is the default behaviour of _oldlist. However, if there is an old list and this style contains the name of the completer function that generated the list, then the old list will be used even if it was generated by a widget which does not do listing. For example, suppose you type ^Xc to use the _correct_word widget, which generates a list of corrections for the word under the cursor. Usually, typing ^D would generate a standard list of completions for the word on the command line, and show that. With _oldlist, it will instead show the list of corrections already generated. As another example consider the _match completer: with the insert-unambiguous style set to `true' it inserts only an unambiguous prefix string if there is any. But since this may remove parts of the original pattern, attempting completion again may result in more matches than on the first attempt. But by using the _oldlist completer and setting this style to _match, the list of matches generated on the first attempt will be used again. The _history_complete_word bindable command uses this style to decide if the available matches should be shown. When using the incremental-complete-word widget, this style says if the matches should be listed on every key press (if they fit on the screen).
list-arguments
Like the arguments style, but used when calling the ps command to generate the list to display.
list-colors
If the zsh/complist module is used, this style can be used to set color specifications as with the ZLS_COLORS and ZLS_COLOURS parameters (see section The zsh/complist Module). If this style is set for the default tag, the strings in the value are taken as specifications that are to be used everywhere. If it is set for other tags, the specifications are used only for matches of the type described by the tag. To be able to share the same specifications one has set up for the GNU version of the ls command one can use:
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
And to get the default colors (which are the same as for the GNU ls command) one should set the style to an empty value.
list-packed
Like the list-colors style, this is tested with the default tag and all tags used when generating matches. If it is set to `true' for a tag, the matches added for it will be listed as if the LIST_PACKED option were set for them. If it is set to `false', they are listed normally.
list-rows-first
This style is tested like the list-packed style and determines if matches are to be listed in a rows-first fashion, as for the LIST_ROWS_FIRST option.
local
This style is used by completion functions which generate URLs as possible matches to add suitable matches when a URL points to a local web server. Its value should consist of three strings: a hostname, the path to the default web pages for the server and the directory name used by a user placing web pages within their home area.
matcher
This style is tested for tags used when generating matches. Its value is used as an additional match specification to use when adding the matches as described in section Matching Control.
max-errors
This is used by the _approximate and _correct completer functions to determine the maximum number of errors to accept. The completer will try to generate completions by first allowing one error, then two errors, and so on, until either a match was found or the maximum number of errors given by this style has been reached. If the value for this style contains the string numeric, the completer function will take any numeric argument as the maximum number of errors allowed. For example, with
zstyle ':completion:approximate' accept 2 numeric
two errors will be allowed if no numeric argument is given. However, with a numeric argument of six (as in `ESC-6 TAB'), up to six errors are accepted. Hence with a value of `0 numeric', no correcting completion will be attempted unless a numeric argument is given. If the value contains the string not-numeric, the completer will not try to generate corrected completions when given a numeric argument, so in this case the number given should be greater than zero. For example, `2 not-numeric' specifies that correcting completion with two errors will usually be performed, but if a numeric argument is given, correcting completion will not be performed.
menu
This style is tested for the default tag and the tags used when adding matches. The value should be one of the `true' values (yes, true, 1, or on) if menu completion should be started when matches for the given tag (or always in case of the default tag) are generated. If none of these values is defined for any of the tags used, but for at least one of these tags the value is the string auto, this says that the same behavior as for the AUTO_MENU option should be used. Finally, if menucompletion is started by some other means (e.g. by setting the MENU_COMPLETE option) and the value for one of the tags used is `false' (i.e. no, false, 0, or off), then menucompletion will not be started for this completions. Note that the values defined for normal tags override the value set for the default tag. Either instead of or in addition to one of the values above, the value for this style may also contain the string select, optionally followed by an equal sign and a number. In this case menuselection (as defined by the zsh/computil module) will be started. Without the optional number, it will be started unconditionally and with a number it will be started only if at least that many matches are generated (if the values for more than one tag defines such a number, the smallest one is taken). Starting menuselection can explicitly be turned off by defining a value containing the string no-select. Note that all this only works if the complist module is explicitly loaded by the user, though. This is also used by the _oldlist completer. Here it controls how menu completion behaves when a completion has already been inserted and the user types a standard completion key type such as TAB. The default behaviour of _oldlist is that menu completion always continues with the existing list of completions. If this style is set to `false', however, a new completion is started if the old list was generated by a different completion command (the behaviour without the _oldlist completer). For example, suppose you type ^Xc to generate a list of corrections, and menu completion is started in one of the usual ways. Usually, typing TAB at this point would start trying to complete the line as it now appears. With _oldlist, it will instead continue to cycle through the list of completions.
numbers
This is used with the jobs tag. If it is `true', the completions will use the job numbers instead of the shortest unambiguous strings of the jobs' command lines. If the value is a number, job numbers will only be used if for at least one of the jobs that many (or more) words from the command line string have to be used to make the strings unambiguous. E.g. if it is set to `1', strings will only be used if all jobs differ in the first word on their command lines.
original
This is used by the _approximate, _correct and _match completers. The first two use it to decide if the original string should be added as one possible completion. Normally, this is done only if there at least two possible corrections, but if this style is set to `true', it will always be added. For the _match completer, if this style is set to only, it will try to generate matches without inserting a `*' at the cursor position. If set to any other non-empty value, it will first try to generate matches without inserting the `*' and if that yields no matches, it will try again with the `*' inserted.
path
This is used together with the the urls tag by completion functions that generate URLs as possible matches. It should be set to the path of a directory containing sub-directories named like `http', `ftp', `bookmark', and so on. These sub-directories should contain files and other sub-directories whose pathnames are possible completions after the initial `http://', `ftp://', etc. See the description in the file _urls in the User sub-directory of the completion system for more information. Also, the function that completes color names uses this style with the colors tag. Here, the value should be the pathname of a file containing color names in the format of an X11 rgb.txt file. That file (if it can be found) will be used as the default if the style is not set.
ports
A style holding the service names of ports to complete. If this is not set by the user, the service names from `/etc/services' will be used.
prefix-hidden
This is used when matches with a common prefix are added (e.g. option names). If it is `true', this prefix will not be shown in the list of matches.
prefix-needed
This, too, is used for matches with a common prefix. If it is set to `true' this common prefix has to be typed by the user to generate the matches. E.g. for options this means that the `-', `+', or `--' has to be on the line to make option names be completed at all.
prompt
The incremental-complete-word widget shows the value of this style in the status line during incremental completion. The sequence `%u' is replaced by the unambiguous part of all matches if there is any and it is different from the word on the line. A `%s' is replaced with `-no match-', `-no prefix-', or an empty string if there is no completion matching the word on the line, if the matches have no common prefix different from the word on the line or if there is such a common prefix, respectively. The sequence `%c' is replaced by the name of the completer function that generated the matches (without the leading underscore). Finally, `%n' is replaced by the number of matches generated, `%a' is replaced by an empty string if the matches are in the normal set (i.e. the one without file names with one of the suffixes from the ignored-suffixes style) and with ` -alt-' if the matches are in the alternate set, and if the list style is set, `%l' is replaced by `...' if the list of matches is too long to fit on the screen and with an empty string otherwise. If the list style is `false', `%l' will always be removed.
remove-all-dups
The _history_complete_word bindable command uses this to decide if all duplicate matches should be removed, rather than just consecutive duplicates.
sort
If set to `true', completion functions that generate words from the history as possible matches sort these words alphabetically instead of keeping them in the order in which they appear in the history (from youngest to oldest). The completion function that generates filenames as possible matches uses this style with the files tag to determine in which order the names should be listed and completed when using menucompletion. The value may be one of size to sort them by the size of the file, links to sort them by the number of links to the file, modification (or time or date) to sort them by the last modification time, access to sort them by the last access time, or inode (or change) to sort them by the last inode change time. Any other value (or not setting this style at all) makes them be sorted alphabetically by name. If the value contains the string reverse, sorting is done in decreasing order. This is also used by the _expand completer. Here, if it is set to `true', the expansions generated will always be sorted. If it is set to menu, then the expansions are only sorted when they are offered as single strings (not in the string containing all possible expansions).
special-dirs
Normally, the completion code will not produce the directory names . and .. as possible completions. If this style is set to `true', it will add both `.' and `..' as possible completions, if it is set to .., only `..' will be added.
squeeze-slashes
If set to `true', sequences of slashes (like in `foo//bar') will be treated as if they were only one slash when completing pathnames.
stop
If set to `true', the _history_complete_word bindable command will always insert matches as if menucompletion were started and it will stop when the last match is inserted. If this style is set to verbose a message will be displayed when the last match is reached.
substitute
If this is unset or set to the empty string, the _expand completer will first try to expand all substitutions in the string (such as `$(...)' and `${...}'). If this is set to an non-empty string it should be an expression usable inside a `$((...))' arithmetical expression. In this case, expansion of substitutions will be done if the expression evaluates to `1'. For example, with
zstyle ':completion:expand' substitute '${NUMERIC:-1} != 1'
substitution will be performed only if given an explicit numeric argument other than `1', as by typing `ESC 2 TAB'.
tag-order
This provides a mechanism for sorting how the tags available in a particular context will be used. The values for the style are sets of space-separated lists of tags. The tags in each value will be tried at the same time; if no match is found, the next value is used. For example,

zstyle :completion:complete::gunzip: tag-order \ 
    'globbed-files directories' all-files

specifies that, when completing arguments of the command gunzip, files generated by patterns (in this case, those ending in .gz) and any directories will be presented first, and if that fails, any other files will be tried. A string starting with an exclamation mark (`!') specifies names of tags that are not to be used -- the effect will be the same as if all other possible tags for the context had been listed. If any string in the value consists of only a hyphen (`-'), then only the tags selected by the other strings will be generated. Normally all tags not explicitly selected will be tried at the end if the selected tags did not generate any matches. This means that a value of only one hyphen turns off completion in a particular context. Strings in the value may also be of the form `func()'. In this case the function func will be called which can then define in which order tags are to be used based on additional context information. See the _sort_tags function below for a description of how such functions can be implemented. If no style has been defined for a context, all tags will be used.
users
This may be set to a list of names that should be completed whenever a username is needed. If it is not set or the string on the line doesn't match any of the strings in this list, all usernames will be completed.
users-hosts
The values of this style should be of the form `user:host'. It is used for commands that need pairs of user- and hostnames. For such commands, only the pairs from this style are used and if, for example, the username is already typed, then only the hostnames for which there is a pair with that username is defined. If set for the my-accounts tag, this is used for commands such as rlogin and ssh. I.e. the style should contain the names of the user's own accounts. With the other-accounts this is used for commands such as talk and finger and should contain other people's accounts. Finally, this may also used by some commands with the accounts tag.
verbose
This is used in several contexts to decide if only a simple or a verbose list of matches should be generated. For example some commands show descriptions for option names if this style is true.
word
To find out if listing should be performed on its own, the _list completer normally compares the contents of the line with the contents the line had at the time of the last invocation. If this style is set to `true', comparison is done using only the current word. In this case, attempting completion on a word equal to the one when completion was called the last time will not delay the generation of matches.

Control Functions

The initialization script compinit redefines all the widgets which perform completion to call the supplied widget function _main_complete. This function acts as a wrapper calling the so-called `completer' functions that generate matches. If _main_complete is called with arguments, these are taken as the names of completer functions to be called in the order given. If no arguments are given, the set of functions to try is taken from the completer style. For example, to use normal completion and correction if that doesn't generate any matches:

zstyle ':completion:*' completer _complete _correct

after calling compinit. The default value for this style set up in compinit is `_complete', i.e. normally only ordinary completion is tried. The _main_complete function uses the return value of the completer functions to decide if other completers should be called. If the return value is zero, no other completers are tried and the _main_complete function returns.

Immediately before returning the _main_complete function calls all functions whose names are given in the comppostfuncs array and then resets it to an empty array. This can be used by completion functions or by other ZLE widgets calling completion to register code that is to be executed after all matches have been added.

The following completer functions are contained in the distribution (users may write their own):

_complete
This completer generates all possible completions in a context-sensitive manner, i.e. using the settings defined with the compdef function explained above and the current settings of all special parameters. To complete arguments of commands, _complete uses the utility function _normal, which is in turn responsible for finding the particular function; it is described below. Various contexts of the form -context-, as mentioned above for the #compdef tag, are handled specially. These are:
-equal-
for completion after an equal sign, other than one occurring in a shell-variable assignment.
-tilde-
for completion after a tilde (`~') character, but before a slash.
-redirect-
for completion after a redirection operator.
-math-
for completion inside mathematical contexts, such as `((...))'.
-subscript-
for completion inside subscripts.
-value-
for completion on the right hand side of an assignment.
-array-value-
for completion on the right hand side of an array-assignment (`foo=(...)').
-condition-
for completion inside conditions (`[[...]]').
-parameter-
for completing the name of a parameter expansion (`$...').
-brace-parameter-
for completing the name of a parameter expansion within braces (`${...}').
-first-
for adding completions before any other completion functions are tried; if this function sets the _compskip parameter to all, no other completion functions will be called, if it is set to a string containing the string patterns, no pattern completion functions will be called, and if it is set to a string containing default the function for the `-default-' context will not be called, but functions defined for commands will.
-default-
for generating completions when no special completion function is used.
-command-
for completing in a command position.
Default implementations are supplied for each of these contexts, in most cases named after the context itself (e.g. completion for the `-tilde-' context is done by the function named `_tilde'). Before trying to find a function for a specific context, _complete checks if the parameter `compcontext' is set to a non-empty value. If it is, the value is taken as the name of the context to use and the function defined for that context will be called.
_approximate
This completer function uses the _complete completer to generate a list of strings for the context the cursor is currently in, allowing you to specify a maximum number of errors: see the description of approximate matching in section Filename Generation for how errors are counted. The resulting list of corrected and completed strings is then presented to the user. The intended use of this completer function is to try after the normal _complete completer by setting:
zstyle ':completion:*' completer _complete _approximate
This will give correcting completion if and only if normal completion doesn't yield any possible completions. When corrected completions are found, the completer will normally start menucompletion allowing you to cycle through these strings. This completer uses the tags corrections and original when generating the possible coprrections and the original string. The format style for the former may contain the additional sequences `%e' and `%o' which will be replaced by the number of errors accepted to generate the corrections and the original string, respectively. Like all completers _approximate uses its name without the undersccore as the top-level context name. Once it has started trying to generate matches, it will add another context name component containing the number of errors accepted in this attempt. So on the first try the context name starts with `:approximate:1', on the second try with `:approximate:2', and so on.
_correct
Generate corrections (but not completions) for the current word; this is similar to spell-checking. This calls _approximate but uses a different top-level context name. For example, with:
zstyle ':completion:*' completer _complete _correct _approximate
zstyle ':completion:correct' accept 2 not-numeric
zstyle ':completion:approximate' accept 3 numeric
correction will accept up to two errors. If a numeric argument is given, correction will not be performed, but correcting completion will be, and will accept as many errors as given by the numeric argument. Without a numeric argument, first correction and then correcting completion will be tried, with the first one accepting two errors and the second one accepting three errors. This completer function is intended to be used without the _approximate completer or, as in the example, just before it. Using it after the _approximate completer is useless since _approximate will at least generate the corrected strings generated by the _correct completer -- and probably more.
_match
This completer is intended to be used after the _complete completer. It allows one to give patterns on the command line and to complete all strings matching these patterns from the set of possible completions for the context the cursor is in, without having to set the GLOB_COMPLETE option. Normally this will be done by taking the pattern from the line, inserting a `*' at the cursor position and comparing the resulting pattern with the possible completions generated. However, if the original style has a value of only, no `*' will be inserted. If original has any other non-empty string as its value, this completer will first try to generate matches without, then with a `*' inserted at the cursor position. The generated matches will be offered in a menucompletion unless the insert-unambiguous style is set to `true'. In this case menucompletion will only be started if no unambiguous string could be generated that is at least as long as the original string. Note that the matcher specifications defined globally or used by the completion functions will not be used.
_expand
This completer function does not really do completion, but instead checks if the word on the command line is eligible for expansion and, if it is, gives detailed control over how this expansion is done. When using this, one should not use the expand-or-complete widget, but instead use complete-word, as otherwise expand-or-complete will expand the string on the line before the completion widget is called. Also, this completer should be called before the _complete completer function. The tags used when generating expansions are all-expansions for the string containing all possible expansions, expansions when adding the possible expansions as single matches and original when adding the original string from the line. In which order these strings are generated and which of these strings are generated at all can be controlled by using the group-order style and by modifying the tag-order style, as usual. The format string for all-expansions and for expansions may contain the sequence `%o' which will be replaced by the original string from the line. In a different mode selected by the completions style, all completions generated for the string on the line are inserted.
_list
This completer allows one to delay the insertion of matches until completion is attempted a second time without the word on the line being changed. On the first attempt, only the list of matches will be shown. Styles used are condition and word, see section Completion System Configuration.
_menu
This completer is a simple example function implemented to show how menucompletion can be done in shell code. It should be used as the first completer and has the effect of making the code perform menucompletion. Note that this is independent of the setting of the MENU_COMPLETE option and does not work with the other menucompletion widgets such as reverse-menu-complete, or accept-and-menu-complete.
_oldlist
This completer controls how the standard completion widgets behave when there is an existing list of completions which may have been generated by a special completion (i.e. a separately-bound completion command). It should appear in the list of completers before any of the widgets which generate matches. It uses two styles: list and menu, see section Completion System Configuration.

Bindable Commands

In addition to the context-dependent completions provided, which are expected to work in an intuitively obvious way, there are a few widgets implementing special behaviour which can be bound separately to keys. The following is a list of these and their default bindings.

_bash_completions
This function is used by two widgets, _bash_complete-word and _bash_list-choices. It exists to provide compatibility with completion bindings in bash. The last character of the binding determines what is completed: `!', command names; `$', environment variables; `@', host names; `/', file names; `~' user names. In bash, the binding preceeded by `\e' gives completion, and preceeded by `^X' lists options. As some of these bindings clash with standard zsh bindings, only `\e~' and `^X~' are bound by default. To add the rest, the following should be added to .zshrc after compinit has been run:
for key in '!' '$' '@' '/' '~'; do
  bindkey "\e$key" _bash_complete-word
  bindkey "^X$key" _bash_list-choices
done
This includes the bindings for `~' in case they were already bound to something else; the completion code does not override user bindings.
_correct_filename (^XC)
Correct the filename path at the cursor position. Allows up to six errors in the name. Can also be called with an argument to correct a filename path, independently of zle; the correction is printed on standard output.
_correct_word (^Xc)
Performs correction of the current argument using the usual contextual completions as possible choices. This uses the top-level context name `:correct-word' and then calls the _correct completer.
_expand_word (^Xe)
Performs expansion on the current word: equivalent to the standard expand-word command, but using the _expand completer. Before calling it, the top-level context name is set to `:expand-word'.
_history_complete_word (\e/)
Complete words from the shell's command history. This uses the list, remove-all-dups, sort, and stop styles.
_most_recent_file (^Xm)
Complete the name of the most recently modified file matching the pattern on the command line (which may be blank). If given a numeric argument N, complete the Nth most recently modified file. Note the completion, if any, is always unique.
_read_comp (^X^R)
Prompt the user for a string, and use that to perform completion on the current word. There are two possibilities for the string. First, it can be a set of words beginning `_', for example `_files -/', in which case the function with any arguments will be called to generate the completions. Unambiguous parts of the function name will be completed automatically (normal completion is not available at this point) until a space is typed. Any other string will be passed as a set of arguments to compadd and should hence be an expression specifying what should be completed. A very restricted set of editing commands is available when reading the string: `DEL' and `^H' delete the last character; `^U' deletes the line, and `^C' and `^G' abort the function, while `RET' accepts the completion. Note the string is used verbatim as a command line, so arguments must be quoted in accordance with standard shell rules. Once a string has been read, the next call to _read_comp will use the existing string instead of reading a new one. To force a new string to be read, call _read_comp with a numeric argument.
_complete_help (^Xh)
This widget displays information about the context names and tags used when completing at the current cursor position.

Utility Functions

Descriptions follow for utility functions that may be useful when writing completion functions. Most of these reside in the Core subdirectory except where noted. Like the example functions for commands in the distribution, the utility functions generating matches all follow the convention of returning zero if they generated completions and non-zero if no matching completions could be added.

_funcall return name [ args ... ]
If a function name exists, it is called with the arguments args. Unless it is the empty string or a single hyphen, return is taken as the name of a parameter and the return status from the called function is stored in it. The return value of _funcall itself is zero if the function name exists and was called and non-zero otherwise.
_compalso names ...
This function looks up the definitions for the context and command names given as arguments and calls the handler functions for them if there is a definition (given with the compdef function). For example, the function completing inside subscripts might use `_compalso -math-' to include the completions generated for mathematical environments.
_normal
This function is used for normal command completion. If completion is attempted on the first word, command names are completed. Otherwise, the arguments are completed by calling the functions defined for this command, including those functions defined for patterns matching the command name. This function can also be called by other completion functions if they have to complete a range of words as a separate command. For example, the function to complete after the pre-command specifiers such as nohup removes the first word from the words array, decrements the CURRENT parameter, then calls this function. When calling a function defined for a pattern, this function also checks if the parameter _compskip is set and uses the value in the same way it is used after calling the completion function for the -first- context. With this one can write a pattern completion function that keeps other functions from being tried simply by setting this parameter to any value.
_description [ -12VJ ] tag name descr [ specs ... ]
This function tests some styles for the tag and and stores options usable for compadd in the array with the given name which guarantee that the matches are generated as requested by the user. The styles tested are: format (which is first tested for the given tag and then for the descriptions tag if that isn't defined), hidden and group-name (the last two are tested only for the tag given as the first argument). This function also calls the _setup function which tests some more styles. The format string from the style (if any) will be modified so that the sequence `%d' is replaced by the descr given as the third argument. If _description is called with more than three arguments, these specs should be of the form `char:str' and every appearance of `%char' in the format string will be replaced by string. The options placed in the array will also make sure that the matches are placed in a separate group, depending on the value of the group-name style. Normally a sorted group will be used for this (with the `-J' option), but if a option starting with `-V', `-J', `-1', or `-2' is given, that option will be included in the array, so that it is possible to make the group unsorted by giving the option `-V', `-1V', or `-2V'. In most cases, this function will be used like this:
local expl
_description expl files file
compadd "$expl[@]" - "$files[@]"
_message descr
The descr is used like the third argument to the _description function. However, the resulting string will always be shown whether or not matches were generated. This is useful to display help texts in places where no completions can be generated automatically. This function also uses the format style for the messages tag in preference to the format style for the descriptions tag. The latter is used only if the former is unset.
_setup tag
This function expects a tag as its argument and sets up the special parameters used by the completion system appropriately for the tag, using styles such as list-colors and last-prompt. Note that this function is called automatically from _description so that one normally doesn't have to call it explicitly.
_tags [ -C name [ tags ... ] ]
If called with arguments, these are taken as the names of the tags for the types of matches the calling completion function can generate in the current context. These tags are stored internally and sorted by using the tag-order style. Following calls to this function without arguments from the same function will then select the first, second, etc. set of tags requested by the user. To test if a certain tag should be tried, the _requested function has to be called (see below). The return value is zero if at least one of the tags is requested and non-zero otherwise. This function also accepts the -C option followed by a name. This name is temporarily (i.e. not visible outside _tags) appended (with a colon before it) to the contents of the curcontext parameter. This allows to make _tags use a more specific context name without having to change and reset the curcontext parameter (which would otherwise have the same effect).
_requested [ -12VJ ] tag name descr [ specs ... ]
A function that uses _tags to register tags and then calls it to loop over the requested sets of tags should call this function to check if a certain tag is currently requested. This normally has to be done in a loop such as:
_tags foo bar baz
while _tags; do
  if _requested foo; then
    ...
  fi
  ... # test other tags
  ... # exit loop if matches were generated
done
So, the first argument for _requested is used as the name of a tag and if that tag is currently requested, the return value is zero (and non-zero otherwise). If more than one argument is given, _requested calls the _description function with all arguments, including the options. This is often useful to do both the testing of the tag and getting the description for the matches one is about to add at once. E.g.:
local expl ret=1
_tags foo bar baz
while _tags; do
  _requested foo expl 'description' &&
      compadd "$expl[@]" foobar foobaz && ret=0
  ...
  (( ret )) || break
done
_wanted [ -12VJ ] tag name descr [ specs ... ]
In many contexts only one type of matches can be generated but even then it should be tested if the tag representing those matches is requested by the user. This function makes that easier. Like _requested it gets arguments as for _description. With the tag it calls _tags and if that returns zero (i.e. the tag is requested by the user) it calls _description. So, if you want to offer only one tag and immediatly want to use the description built, you can just do:
_wanted tag expl 'description' &&
    compadd "$expl[@]" matches...
Note that you only need to use this function if you need a description. If, for example, you use one of the utility functions that adds a description itself, you only need to call _tags as in:
_tags tag && _helper
_alternative [ -C name ] specs ...
This function is useful if you offer multiple tags and building the matches for them is easy enough. It basically implements a loop like the one described above. The tags to use and what to do if the tags are requested are described using the specs which are of the form: `tag:descr:action'. The tags are offered using _tags and if the tag is requested, the action is executed with the given descr (description). The actions supported are those used by the _arguments function (described below), without the `->state' form. For example, the action may be a simple function call. With that one could do:
_alternative 'users:user:_users' 'hosts:host:_hosts'
to offer usernames and hostnames as possible matches (which are generated by the _users and _hosts functions respectively). Like _tags this function supports the -C option to give an additional context name component.
_describe descr name1 [ name2 ] opts ... -- ...
This function can be used to add options or values with descriptions as matches. The descr is taken as a string to display above the matches if the format style for the descriptions tag is set. After this one or two names of arrays followed by options to give to compadd must be given. The first array contains the possible completions with their descriptions (with the description separated by a colon from the completion string). If the second array is given, it should have the same number of elements as the first one and these elements are added as possible completions instead of the strings from the first array. In any case, however, the completion list will show the strings from the first array. Any number of array/option sequences may be given separated by `--'. This allows one to display matches together that need to be added with different options for compadd. Before the first argument, the option `-o' may be given. It says that the matches added are option names. This will make _describe use the prefix-hidden, prefix-needed and verbose styles to find out if the strings should be added at all and if the descriptions should be shown. Without the `-o' option, only the verbose style is used.
_multi_parts sep array
This function gets two arguments: a separator character and an array. As usual, the array may be either the name of an array parameter or a literal array in the form `(foo bar)' (i.e. a list of words separated by white space in parentheses). With these arguments, this function will complete to strings from the array where the parts separated by the separator character are completed independently. For example, the _tar function from the distribution caches the pathnames from the tar file in an array and then calls this function to complete these names in the way normal filenames are completed by the _path_files function. Like other utility functions, this function accepts the `-V', `-J', `-1', `-2', `-n', `-X', `-M', `-P', `-S', `-r', `-R', and `-q' options and passes them to the compadd builtin.
_sep_parts
This function gets as arguments alternating arrays and separators. The arrays specify completions for parts of strings to be separated by the separators. The arrays may be the names of array parameters or a quoted list of words in parentheses. For example, with the array `hosts=(ftp news)' the call `_sep_parts '(foo bar)' @ hosts' will complete the string `f' to `foo' and the string `b@n' to `bar@news'. This function passes the `-V', `-J', `-1', `-2', `-n', `-X', `-M', `-P', `-S', `-r', `-R', and `-q' options and their arguments to the compadd builtin used to add the matches.
_path_files and _files
The function _path_files is used throughout the shell code to complete filenames. It allows completion of partial paths. For example, the string `/u/i/s/sig' may be completed to `/usr/include/sys/signal.h'. The option `-/' specifies that only directories should be completed. The option `-g pattern' says that only files matching the pattern should be completed, and the `-f' option, which is the default, completes all filenames. The option `-W paths' may be used to specify path prefixes that are to be prepended to the string from the line to generate the filenames but that should not be inserted in the line or shown in a completion listing. The paths may be the name of an array parameter, a literal list of paths enclosed in parentheses or an absolute pathname. Additionally, the `-F' option from the compadd builtin is supported, giving direct control over which filenames should be ignored. If no such option is given, the ignored-suffixes style is used. The function _files calls _path_files with all the arguments it was passed and, if that generated no matches, calls _path_files again without any -g or -/ option, thus generating all filenames. These functions also accept the `-J', `-V', `-1', `-2', `-n', `-X', `-M', `-P', `-S', `-q', `-r', and `-R' options from the compadd builtin. Finally, the _path_files function uses the styles expand, cursor and special-dirs with the paths tag.
_parameters
This should be used to complete parameter names. All arguments are passed unchanged to the compadd builtin.
_options
This can be used to complete option names. It uses a matching specification that ignores a leading `no', ignores underscores and allows the user to type upper-case letters, making them match their lower-case counterparts. All arguments passed to this function are propagated unchanged to the compadd builtin.
_set_options and _unset_options
These functions complete only set or unset options, with the same matching specification used in the _options function. Note that you need to uncomment a few lines in the _main_complete function for these functions to work properly. The lines in question are used to store the option settings in effect before the completion widget locally sets the options it needs.
_arguments specs ...
This function resides in the Base subdirectory of the example completion system because it is not used by the core system. This function can be used to complete words on the line by simply describing the arguments the command on the line gets. The description is given as arguments to this function, with each spec describing one option or normal argument of the command. The descriptions understood are:
n:message:action
This describes the n'th normal argument. The message will be printed above the matches generated and the action says what can be completed in this position (see below). If there are two colons before the message, this describes an optional argument.
:message:action
Like the previous one, but describing the next argument. I.e. if you want to describe all arguments a command can get, you can leave out the numbers in the description and just use this form to describe them one after another in the order they have to appear on the line.
*:message:action
This describes how arguments are to be completed for which no description with one of the first two forms was given. This also means that any number of arguments can be completed. If there are two colons before the message (as in `*::message:action') the words special array and the CURRENT special parameter will be restricted to only the normal arguments when the action is executed or evaluated. With three colons before the message they will be restricted to only the normal arguments covered by this description.
opt-spec[description ...]
This describes an option and (if at least one description is given) the arguments that have to come after the option. If no description is given, this will only be used to offer the option name as a possible completion in the right places. Each description has to be of the form `:message:action' or `::message:action', where the second form describes an optional argument and the first one describes a mandatory argument. The last description may also be of the form `:*:message:action' or `:*pattern:message:action'. These describe multiple arguments. In the first form all following words on the line are to be completed as described by the action, in the second form all words up to a word matching the given pattern are to be completed using the action. The `*' or the pattern may also be separated from the message by two or three colons. With two colons the words special array and the CURRENT special parameter are modified to refer only to the words after the option (with two colons) or to the words covered by this description (with three colons) during the execution or evaluation of the action. Note that only one such `:*'-specification is useful and no other argument specification may be given after it. In the simplest form the opt-spec is just the option name beginning with a minus or a plus sign, such as `-foo'. In this case, the first argument for the option (if any) has to come as a separate word directly after the option and the option may appear only once on the line (and if it is already on the line, the option name will not be offered as a possible completion again). If the first argument for the option has to come directly after the option name in the same word, a minus sign should be added to the end of the opt-spec, as in `-foo-'. If the first argument may be given in one string with the option name, but may also be given as a separate argument after the option, a plus sign should be used instead. If the argument may be given as the next string or in same string as the option name but separated from it by an equal sign, a `=' should be used instead of the minus or plus sign. If the option may be given more than once, a star (`*') has to be added in front of the opt-spec. An opt-spec may also contain a list of other option names with which the option described is mutually exclusive. Such a list is given in parentheses at the beginning, as in `(-two -three)-one:...'. In this example, the options `-two' and `-three' will not be offered as possible completions if the option `-one' is on the line. Also, the list may contain a single colon as one of its elements to specify that the descriptions for normal (non-option-) arguments should not be used if the option described is on the line. Finally, the opt-spec may contain a explanation string. This is given in brackets at the end, as in `-q[query operation]'. The verbose style is used to decide if these explanation strings should be printed when options are listed. If no explanation string is given but the auto-describe style is set and only one argument is described for this opt-spec, the option will be described by the value of the style with any appearance of the sequence `%d' in it replaced by the description for the first argument.
In each of the cases above, the action says how the possible completions should be generated. In cases where only one of a fixed set of strings can be completed, these strings can directly be given as a list in parentheses, as in `:foo:(foo bar baz)'. Such a list in doubled parentheses, as in `:foo:((a\:bar b\:baz))' should contain strings consisting of the string to complete followed by a colon (which needs to be preceded by a backslash) and a description. The matches will be listed together with their descriptions if the description style for the values tag is set. An action of the form `->string' is used by functions that implement a state machine. In this case, the `string' (with all leading and trailing spaces and tabs removed) will be stored in the global parameter state and the function returns with a return value of 300 (to make it distinguishable from other return values) after setting the global `context', `line' and `opt_args' parameters as described below and without resetting any changes made to the special parameters such as PREFIX and words. Note that this means that a function calling _arguments with at least one action containing such a `->string' has to declare appropriate local parameters as in:
local context state line
typeset -A opt_args
This will ensure that _arguments does not create unused global parameters. A string in braces will be evaluated to generate the matches and if the action does not begin with an opening parentheses or brace, it will be split into separate words and executed. If the action starts with a space, this list of words will be invoked unchanged, otherwise it will be invoked with some extra strings placed after the first word which can be given as arguments to the compadd builtin command and which make sure that the message given in the description will be shown above the matches. These arguments are taken from the array parameter expl which will be set up before executing the action and hence may be used in it (normally in an expansion like `$expl[@]'). In places where no sensible matches can be generated, the action should consist of only a space. This will make the message be displayed but no possible completions listed. Note that even in this case the colon at the end of the message is needed. The only case where it can be left is when neither a message, nor a action is given. To include a colon in the option name, the message or the action, it has to be preceded by a backslash. During the evaluation or execution of the action the array `line' will be set to the command name and normal arguments from the command line, i.e. to the words from the command line excluding all options and their arguments. These are stored in the associative array `opt_args', using the option names as keys and their arguments as the values. For options that have more than one argument these are given as one string, separated by colons. All colons in the original arguments are preceded with backslashes. The parameter `context' will be set to the automatically created context name. This is either a string of the form `-opt-n' for the n'th argument of the option -opt, or a string of the form `argument-n' for the n'th argument (for rest arguments the n is the string `rest'). For example, when completing the argument of the -o option, the name is `-o-1' and for the second normal (non-option-) argument it is `argument-2'. Also, during the evaluation of the action, the context name in the curcontext parameter will be changed by appending the same string that is stored in the context parameter. Normally the option names are taken as multi-character names and a word from the line is considered to contain only one option (or none). By giving the -s option to this function (before the first description), options are considered to be one-character options and the strings from the line may contain more than one such option letter. However, strings beginning with two hyphens (like `--prefix') are still considered to contain only one option name. This allows the use of the `-s' option to describe single-letter options together with such long option names. Another option supported is `-O name'. The name will be taken as the name of an array and its elements will be given to functions called to generate matches when executing the actions. For example, this allows one to give options for the compadd builtin that should be used for all actions. Also, the -M option followed by a string may be given before the first description. The string will be used as the match specification when completing option names and values instead of the default `r:|[_-]=* r:|=*'. Finally, the option -C can be given to make _arguments modify the curcontext parameter when a action of the form `->state' is used. This parameter is used to keep track of the current context and in this case it (and not the parameter context as explained above) has to be made local to make sure that calling functions don't use the modified value. Also, the local version of curcontext has to be initialised with the old value as in:
local curcontext="$curcontext"
The function can also be made to automatically complete long options for commands that support the `--help' option as, for example, most of the GNU commands do. For this, the string `--' must be given as one argument and if it is, the command from the line is invoked with the `--help' option and its output is parsed to find possible option names. Note that this means that you should be careful to make sure that this feature is not used for a command that does not support this option. For such automatically found options that get an argument after a `=', the function also tries to automatically find out what should be completed as the argument. The possible completions for option-arguments can be described with the arguments after the `--' (which are not used as described above). Each argument contains one description of the form `pattern:message:action'. The message and the action have the same format as for the normal option descriptions described above. The action will be executed to complete arguments of options whose description in the output of the command from the line with the `--help' option matches the pattern. For example:
_arguments -- '*\*:toggle:(yes no)' \ 
              '*=FILE*:file:_files' \ 
              '*=DIR*:directory:_files -/'
Here, `yes' and `no' will be completed as the argument of options whose description ends in a star, file names for options that contain the substring `=FILE' in the description, and paths for options whose description contains `=DIR'. In fact, the last two patterns are not needed since this function always completes files for option descriptions containing `=FILE' and paths for option descriptions that contain `=DIR' or `=PATH'. These builtin patterns can be overridden by patterns given as arguments, however. Note also that _arguments tries to find out automatically if the argument for an option is optional. If it fails to automatically detect this, the colon before the message can be doubled to tell it about this as described for the normal option descriptions above. The option `-i patterns' (which must be given after the `--') can be used to give patterns for options which should not be completed. The patterns can be given as the name of an array parameter or as a literal list in parentheses. E.g. `-i "(--(en|dis)able-FEATURE*)"' will make the options `--enable-FEATURE' and `--disable-FEATURE' be ignored. The option `-s pairs' (again, after the `--') can be used to describe option aliases. Each pair consists of a pattern and a replacement. E.g. some configure-scripts describe options only as `--enable-foo', but also accept `--disable-foo'. To allow completion of the second form, one would use `-s "(#--enable- --disable-)"'. Example:
_arguments '-l+:left border:' \ 
           '-format:paper size:(letter A4)' \ 
           '*-copy:output file:_files::resolution:(300 600)' \ 
           ':postscript file:_files -g *.(ps|eps)' \ 
           '*:page number:'
This describes three options: `-l', `-format', and `-copy'. The first one gets one argument described as `left border' for which no completion will be offered because of the empty action. The argument may come directly after the `-l' or it may be given as the next word on the line. The `-format' option gets one argument (in the next word) described as `paper size' for which only the strings `letter' and `A4' will be completed. The `-copy' option differs from the first two in that it may appear more than once on the command line and in that it accepts two arguments. The first one is mandatory and will be completed as a filename. The second one is optional (because of the second colon before the description `resolution') and will be completed from the strings `300' and `600'. The last two descriptions say what should be completed as arguments. The first one describes the first argument as a `postscript file' and makes files ending in `ps' or `eps' be completed. The last description says that all other arguments are `page numbers' but does not give possible completions.
_values specs ...
This is used to complete values (strings) and their arguments or lists of such values. If the first argument is the option `-O name', this will be used in the same way as by the _arguments function. I.e. the elements of the name array will be given to calls to compadd and when executing an action. Otherwise, if the first argument (or the first argument after the `-O name' option if that is used) is the option `-s', the next argument is used as the character that separates multiple values. The first argument (after the options and separator character if they are given) is used as a string to print as a description before listing the values. All other arguments describe the possible values and their arguments in the same format used for the description of options by the _arguments function (see above). The only difference is that there is no required minus or plus sign at the beginning and that values can have only one argument. Example:
_values -s , 'description' \ 
        '*foo[bar]' '(two)*one[number]:first count:' \ 
        'two[another number]::second count:(1 2 3)'
This describes three possible values: `foo', `one', and `two'. The first one is described as `bar', gets no argument and may appear more than once. The second one is described as `number', may appear more than once, and gets one mandatory argument described as `first count' for which no action is specified so that it will not be completed automatically. The `(two)' at the beginning says that if the value `one' is on the line, the value `two' will not be considered to be a possible completion anymore. Finally, the last value (`two') is described as `another number' and gets an optional argument decribed as `second count' which will be completed from the strings `1', `2', and `3'. The _values function will complete lists of these values separated by commas. Like _arguments this function temporarily adds another context name component to the current context name while executing the action. Here this name is just the name of the value for which the argument is completed. To decide if the descriptions for the values (not those for the arguments) should be printed, the verbose is used. One last difference to _arguments is that this function uses the associative array val_args to report values and their arguments (but otherwise this is the same as the opt_args association used by _arguments). This also means that the function calling _values should declare the state, line, context and val_args parameters as in:
local context state line
typeset -A val_args
when using an action of the form `->string'. With this function the context parameter will be set to the name of the value whose argument is to be completed. Like _arguments, _values also supports the -C option in which case you have to make the parameter curcontext local instead of context (as described above).
_regex_arguments name specs ...
This function is a compiler to generate a completion function. The first argument specifies the name of a generated function while the remaining arguments specify a completion as a set of regular expressions with actions. The generated function has the structure of a finite-state machine whose state corresponds to the state (i.e. the context) of the completion. This state machine uses a command line, which comes from concatentating the words array up to the current cursor position using null characters as a separator with no extra quotation. This is analysed and at the end the appropriate action is executed. Specification arguments take one of following forms, in which metacharacters such as `(', `)', `#' and `|' should be quoted.
/pattern/ [%lookahead%] [-guard] [:action]
This is a primitive element, corresponding to one state of the compiled state machine. The state is entered if the pattern `(#b)((#B)pattern)(#B)lookahead*' matches the command line string. If it is matched, `guard' is evaluated and its return status is examined; if this is successful, the state is entered, else the test fails and other candidates are tried. The pattern string `[]' is guaranteed never to match. If the test succeeds and the state is entered, the left part of the command line string matched as `pattern' is removed and the next state is tried, proceeding from inside to outside and from left to right. If no test succeeds and the remaining command line string contains no null character, the completion target is restricted to the remainder of the command line string and `action's for the target are evaluated. In this case, nothing is actually removed from the command line string so that any previous or neighbouring state may also have `actions's.
/pattern/+ [%lookahead%] [-guard] [:action]
This is similar to `/pattern/ ...' but the left part of command line string is also considered as part of the completion target.
/pattern/- [%lookahead%] [-guard] [:action]
This is similar to `/pattern/ ...' but `action's of the current and previous states are ignored even if the following state's `pattern' matches the empty string.
( spec )
This groups `spec'.
spec #
This allows any number of repetitions of `spec'.
spec spec
This represents the concatenation of two `spec's.
spec | spec
Either of two `spec's can be matched.
_combination [ -s spec ] tag style specs ... field opts ...
This function is used to complete combinations of values such as pairs of hostnames and usernames. The possible values will be taken from the style whose name is given as the second argument. The first argument is the tag to use to do the lookup. The style name should consist of multiple parts separated by hyphens which are then used as fieldnames. Known values for such fields can be given after the second argument in arguments of the form `field=pattern'. The first argument without a equal sign is taken as the name of the field for which completions should be generated. The matches generated will be taken from the value of the style. These values should contain the possible values for the combinations where the values for the different fields are separated by colons or the character given after the -s option to _combination. Only the values for the requested fields for which the patterns given in the `field=pattern' match the respective fields in the strings from the style value are generated as possible matches. If no style with the given name is defined for the given tag but a function named with the name of the requested field preceded by an underscore is defined, that function will be called to generate the matches. This is also done if none of the strings in the value of the style match all the patterns given as arguments. If the same name is used for more than one field, in both the `field=pattern' and the argument that gives the field name to complete for, the number of the field (starting with one) may be given after the fieldname, separated from it by a colon. All arguments after the requested fieldname are given to the compadd used (when generating matches from the style value) and to the functions for the fields if they are called.
_sort_tags tag ...
As described above for the tag-order style, this is only provided to show how functions that sort tags can be implemented. Inside such functions the name of the current context can be accessed using the curcontext parameter. For example, the function generating file names (called _files) in the completion system is often called to generate only filenames matching a given glob pattern, in which case it uses the tags globbed-files, directories, and all-files. This means that the function offers to generate filenames matching the pattern, names of directories or all filenames as possible matches. Example:
_sort_tags() {
  case $curcontext in
  (*::dvips:*)
    comptry globbed-files directories
    comptry all-files
    ;;
  (*)
    comptry globbed-files
    comptry directories
    comptry all-files
    ;;
  esac
}
Every call to the comptry function (actually a builtin command defined by the zsh/computil module) gives a set of tags to use; as soon as comptry produces some matches, subsequent calls have no effect. Hence in the example this means that for the dvips command on the first attempt the names of DVI files and directories will be generated (first call to comptry). If none of those names match the string from the command line the completion function will generate all filenames as possible matches (second call to comptry). For all other context names the second case-pattern matches, so that normally the completion functions will only try the filenames matching the glob pattern (if any glob pattern is used). If that doesn't yield any matches, names of directories are generated, and if that doesn't yield any matching names either, all filenames will be generated. In every context the function may call comptry as often as it wants. Also, every string may be given as argument, even if no tag with such a name was offered by the completion function. This allows one to give a preferred ordering for some common tag sets without having to worry about sensible patterns for context names. For example, many completion functions can generate both arguments and option names for commands. These functions normally use the tags arguments and options. Depending on your preference you may write in your sorting function:
_sort_tags() {
  comptry arguments options
  case $curcontext in
  ...
  esac
}
or
_sort_tags() {
  comptry arguments
  comptry options
  case $curcontext in
  ...
  esac
}
The former always adds both the matches for the argument and the option names as possible matches. The latter forces matches for the arguments to be preferred. In this case option names are only generated as matches if the string on the line matches no possible completion for the argument, which normally means that you have to type the hyphen the option names start with yourself to see the list of option names that can be completed. Since the completion functions are free to choose the tag names they use, there can't be a complete list. So to make sure that all types of matches are eventually tried as completions, one should use a call to comptry with all arguments at the end of the sorting function. For those contexts where one really wants to make sure that certain tags are never used one can then use a call to return to circumvent that last comptry. For example:
_sort_tags() {
  ...
  case $curcontext in
  (*::kill:*)
    comptry processes
    return
    ;;
  esac
  comptry "$@"
}
The completion function for the kill builtin command offers the tags jobs and processes which represent job references (e.g. `%1') and process identifiers respectively. The function above makes sure that for this builtin command only process identifiers are generated as possible matches by using only the processes tag in a call to comptry. The immediate call to return then makes sure that the default comptry at the end is not executed.

Completion Directories

In the source distribution, the files are contained in various subdirectories of the Completion directory. They may have been installed in the same structure, or into one single function directory. The following is a description of the files found in the original directory structure. If you wish to alter an installed file, you will need to copy it to some directory which appears earlier in your fpath than the standard directory where it appears.

Core
The core scripts and functions. You will certainly need these, though will probably not need to alter them. Many of these are documented above.
Base
Other functions you will almost certainly want if you are going to use any of the standard completion functions. You may want to edit some of these files.
Builtins
Functions for completing arguments of shell builtin commands and utility functions for this (which are also used by functions from the User directory).
User
Functions for completing arguments of external commands and suites of commands. They may need modifying for your system.
Commands
Functions which implement special types of completion to be bound to keystrokes rather than called by context.


Go to the first, previous, next, last section, table of contents.