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 `compctl -M ...' mechanism 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.
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'.
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.
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:
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.
Note that the # is part of the tag name and no white space is allowed after it. The #compdef tags use the compdef function defined below; the main difference is that the name of the function is supplied implicitly.
The compinit file defines the following functions, which may also be called directly by the user.
The third form 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 fourth form 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 complist module is loaded. The option -n prevents the key being bound if it is already to bound to something other than undefined-key.
In each of the forms supporting it the -a option makes the function autoloadable (exactly equivalent to autoload function).
Each definition may be either a simple `key', which sets this key in the compconfig array to an empty string, or of the form `key=value' which stores the `value' under key `key'.
Since the completion system also uses the array for internal purposes, you should not set all values at once by doing `compconfig=(...)'.
In the second form (without arguments), this function lists all keys and their values. If given the -l option as its first argument, as in the last form, the other arguments are taken as names of keys and the values of these keys are printed one per line. In either case, if the -L option is given, the keys and values are printed as calls to this function, usable to be put in a setup script.
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 colon-separated list in the configuration key completer. For example, to use normal completion and correction if that doesn't generate any matches:
compconf completer=_complete:_correct
after sourcing compinit. The default value for this configuration key 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.
The widget function _main_complete also uses the configuration key last_prompt. If this is set to always, the cursor is moved up to the last prompt after printing a list of matches even if a numeric argument was given.
The following completer functions are contained in the distribution (users may write their own):
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:
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.
compconf 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.
The exact behavior of this completer can be changed by using the following configuration keys:
If the value for this key contains a lower- or upper-case `n', the completer function will take any numeric argument as the maximum number of errors allowed. For example, with
compconf approximate_accept=2n
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 `0n', no correcting completion will be attempted unless a numeric argument is given.
If the value contains `n' or `N' and an exclamation mark (`!'), _approximate 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, `2n!' specifies that correcting completion with two errors will usually be performed, but if a numeric argument is given, correcting completion will not be performed.
If any of these keys is not set, but the the same key with the prefix `correct' instead of `approximate' is set, that value will be used. The forms beginning with `correct' are also used by the _correct completer function.
The keys with the `approximate' prefix have no default values, but compinit defines default values for correct_accept (which is set to `2n'), and correct_prompt.
For example, with:
compconf completer=_complete:_correct:_approximate compconf correct_accept='2n!' approximate_accept=3n
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.
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 configuration key match_original has a value of `only', no `*' will be inserted. If match_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 match_insert configuration key is set to a string starting with `unambig'. 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.
Control over how the expanded string will be treated is possible with the following configuration keys:
compconf 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'.
None of these configuration keys has a default value.
compconf list_condition='NUMERIC != 1'
delaying will be done only if given an explicit numeric argument other than `1'.
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 match_insert key set to unambig 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 key to _match, the list of matches generated on the first attempt will be used again.
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.
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.
When calling a function defined for a pattern, this function also checks if the parameter _compskip is set. If it was set by the function called, no further completion functions are called. With this one can write a pattern completion function that keeps other functions from being tried simply by setting this parameter to any value.
Like other utility functions, this function accepts the `-V', `-J', and `-X' options with an argument and passes them to the compadd builtin.
This function passes the `-V', `-J', and `-X' options and their arguments to the compadd builtin used to add the matches.
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', `-X', `-P', `-S', `-q', `-r', and `-R' options from the compadd builtin.
Finally, the _path_files function supports three configuration keys.
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.
This function is used to complete long options for commands that support the `--help' option as, for example, most of the GNU commands do. For this it invokes the command from the line with the `--help' option and then parses the output to find possible option names. Note that this means that you should be careful to make sure that this function is not called for a command that does not support this option.
For options that get an argument after a `=', the function also automatically tries to find out what should be completed as the argument. The possible completions for option-arguments can be described with the arguments to this function. This is done by giving pairs of patterns and actions as consecutive arguments. The actions specify what should be done to complete arguments of those options whose description match the pattern. The action may be a list of words in brackets or in parentheses, separated by spaces. A list in square brackets denotes possible values for an optional argument, a list in parentheses gives words to complete for mandatory arguments. If the action does not start with a square bracket or parenthesis, it should be the name of a command (probably with arguments) that should be invoked to complete after the equal sign. Example:
_long_options '*\*' '(yes no)' \ '*=FILE*' '_files' \ '*=DIR*' '_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.
This function also accepts the `-X', `-J', and `-V' options which are passed unchanged to `compadd'. If the option `-t' is given, completion is only done on words starting with two hyphens. The option `-i patterns' 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 ignore the options `--enable-FEATURE' and `--disable-FEATURE'. Finally, the option `-s pairs' can be used to describe options 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-)"'.
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.
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.
Otherwise, any other string, for example `-b', will be passed as arguments to compgen and should hence be a set of flags specifying the type of completion.
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.