[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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'.
19.2 Initialization 19.3 Completion System Configuration 19.4 Control Functions 19.5 Bindable Commands 19.6 Utility Functions 19.7 Completion Directories
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If the system was installed completely, it should be enough to call the shell function compinit from your initialization file; see the next section. However, the function compinstall can be run by a user to configure various aspects of the completion system.
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, although lines using the command `zstyle' should be gracefully handled.
The new code will take effect next time you start the shell, or run .zshrc by hand; there is also an option to make them take effect immediately. However, if compinstall has removed definitions, you will need to restart the shell to see the changes.
To run compinstall you will need to make sure it is in a directory mentioned in your fpath parameter, which should already be the case if zsh was properly configured as long as your startup files do not remove the appropriate directories from fpath. Then it must be autoloaded (`autoload -U compinstall' is recommended). You can abort the installation any time you are being prompted for information, and your .zshrc will not be altered at all; changes only take place right at the end, where you are specifically asked for confirmation.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 parameter, and should be autoloaded (`autoload -U compinit' is recommended), and then run simply as `compinit'. This will define a few utility functions, arrange for all the necessary shell functions to be autoloaded, and will then re-define all widgets that do completion to use the new system. If you use the menu-select widget, which is part of the zsh/complist module, you should make sure that that module is loaded before the call to compinit so that that widget is also re-defined. If completion styles (see below) are set up to perform expansion as well as completion by default, and the TAB key is bound to expand-or-complete, compinit will rebind it to complete-word; this is necessary to use the correct form of expansion.
Should you need to use the original completion commands, you can still bind keys to the old widgets by putting a `.' in front of the widget name, e.g. `.expand-or-complete'.
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, it will read the dumped file instead of performing 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 compinit will re-create it the next time it is run. The check performed to see if there are new functions can be omitted by giving the option -C. In this case the dump file will only be created if there isn't one already.
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.
For security reasons compinit also checks if the completion system would use files not owned by root or by the current user, or files in directories that are world- or group-writable or that are not owned by root or by the current user. If such files or directories are found, compinit will ask if the completion system should really be used. To avoid these tests and make all files found be used without asking, use the option -u, and to make compinit silently ignore all insecure files and directories use the option -i. This security check is skipped entirely when the -C option is given.
The security check can be retried at any time by running the function compaudit. This is the same check used by compinit, but when it is executed directly any changes to fpath are made local to the function so they do not persist. The directories to be checked may be passed as arguments; if none are given, compaudit uses fpath and _compdir to find completion system directories, adding missing ones to fpath as necessary. To force a check of exactly the directories currently named in fpath, set _compdir to an empty string before calling compaudit or compinit.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 the standard functions.
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 that directory has a subdirectory named Base, all subdirectories will be added to the path. Furthermore, if the subdirectory Base has a subdirectory named Core, compinit will add all subdirectories of the subdirectories 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:
Each name may also be of the form `cmd=service'. This is used by functions that offer multiple services, i.e. different completion behaviour for multiple commands. Such a string makes the completion system call the function when completing arguments for the command `cmd', setting the parameter $service to the string `service'. The function can then use that parameter to decide what to complete.
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. Note that a key will not be re-bound if if it already was (that is, was bound to something other than undefined-key). The widget created has the same name as the file and can be bound to any other keys using bindkey as usual.
#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'.
The options will be given to the autoload builtin command when making the function autoloaded. Most often, this will be +X to force the function to be loaded immediately. Note that the -U flag is always implicitly added.
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.
Note also that the functions for the completion system assume that the KSH_AUTOLOAD option is not set and cannot be loaded when it is set. To avoid having to unset KSH_AUTOLOAD, you can instead use one or more zwc file(s) which have been created with the command zcompile -z to load the functions for the completion system; see 16. Shell Builtin Commands. This forces the functions to be autoloaded the way zsh normally loads functions.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The compinit file defines the following function, which may also be called directly by the user.
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).
The compdef function is the place to turn to when one wants to define what the completion system should complete for a certain command. The function named can of course be one of the functions supplied or one written by the user. For example, if one has a command foo that gets process identifiers as arguments, one could do:
compdef _pids foo |
using the _pids function from the distribution to generate the process identifiers. Not also the _gnu_generic function described below, which can be used to complete options for commands that understand the `--help' option.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 possibly also the name of an option when completing an argument to that option.
The `context' of a completion is a string consisting of multiple fields. This is used to look up styles that can be used to configure the completion system. Since it is not possible to build the whole context string in advance, completion functions may modify some of the fields and hence the context used for lookup may vary during the same call to the completion system.
The context string always consists of the following fields, separated by colons and with a leading colon before the first:
As an example, the context name
:completion::complete:dvips:option-o-1:files |
says that normal completion was attempted on an argument of the dvips command (more precisely: completion was attempted on the first argument after the -o option) and the completion function will generate filenames for this context.
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, although the default is to try all relevant tags in an order determined by the particular completion in use.
The _complete_help bindable command described in 19.5 Bindable Commands can be invoked to find out the context and tag names and styles used at a particular point in completion. It shows the list of contexts and tags that would be used in 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 (21.23 The zsh/zutil Module). When looking up styles the completion system uses full context names, including the tag.
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 use the verbose form, put
zstyle ':completion:*' verbose yes |
in one of the startup files like .zshrc; this sort of style can also be configured with the compinstall function. This definition simply means that the verbose style has yes as its value in every context inside the completion system. If the context pattern were `*', the verbose style would have this value anywhere the style mechanism is used, not just in completion.
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 |
Furthermore, if one wanted to see the command lines for processes but not the job texts one could use the fact that the context name contains the tag name when styles are looked up. As the function for the kill builtin command uses the tags jobs and processes, we can use:
zstyle ':completion:*:*:kill:*:jobs' verbose no |
To have more control over when certain values for styles are used one can use the special parameters available in completion widgets (see 18. Completion Widgets)) and the -e option to zstyle that makes the value be evaluated when looked up. For example, to make the completer style have a different value when completing for the cvs command, one could use the words special array:
zstyle -e ':completion:*' completer ' if [[ $words[1] = cvs ]]; then reply=(_complete) else reply=(_complete _approximate) fi' |
One should be careful not to use too complicated code with this option, at least for the styles that are looked up quite often. These are basically those that define some global completion behaviour but allow that to be different for all matches or groups of matches (such as the menu and list-rows-first styles). Alternatively one can always use a less general pattern for the context than in the example above and use a second call to zstyle with a generic pattern and without using the -e option to define the default behaviour.
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 with tags, completion functions can use any style they choose, 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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are the tags currently used by the completion system. Some of them are only used when looking up styles and do not refer to a particular type of match.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are the names of the styles used by the completion system. Note that the values of several of these styles represent boolean values; here, 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; in particular, the default value may be either on or off if the style is not set.
Some of these styles are tested for every tag used to add possible matches and for the default tag (most notably menu, list-colors and the styles controlling the completion listing like list-packed and last-prompt). When tested for the default tag, only the function field of the context will be set up, so the default value will normally be set like:
zstyle ':completion:*:default' menu ... |
When completing pathnames (where it is looked up for the paths tag), this style also accepts any number of patterns as the value. If this is used, pathnames matching one of these patterns will be accepted immediately even if the command line contains some more partially typed pathname components and these match no file under the directory accepted.
Note that this is also used by the _expand completer to decide if words beginning with a tilde or parameter expansion should be expanded. This means that if, for example, there are parameters foo and foobar, the string `$foo' will only be expanded if accept-exact is set to `true'.
It is also used by the _prefix completer as a simple boolean value to decide if a space should be inserted before the suffix.
The default is to complete lists when the word on the line already contains a colon.
The default value for this style is `_expand _old_list _correct _approximate', i.e. it contains the completers for which a string with all matches will almost never be wanted.
As an example, the function generating process IDs as matches uses this style with the processes tag to generate the IDs to complete and the list of processes to display (if the verbose style is `true'). The list produced by the command should look like the output of the ps command. The first line is not displayed, but is searched for the string `PID' (or `pid') to find the position of the process IDs in the following lines. If the line does not contain `PID', the first numbers in each of the other lines are taken as the process IDs to complete.
Note that the completion function generally has to call the command every time it is called. Because of that care should be taken to specify only commands that take a short time to run (and that will eventually stop at all).
Each string may be the name of a completer function or a string of the form `function:name'. In the first case the completer field of the context will contain the name of the completer without the leading underscore and with all other underscores replaced by hyphens. In the second case the function is the name of the completer to call, but the context will contain the name in the completer field of the context. If the name starts with a hyphen, the string for the context will be build from the name of the completer function as in the first case with the name appended to it. For example:
zstyle ':completion:*' completer _complete _complete:-foo |
Here, completion will call the _complete completer twice, once using `complete' and once using `complete-foo' in the completer field of the context. Normally, using the same completer more than once makes only sense when used with the `functions:name' form, because otherwise the context name will be the same in all calls to the completer; possible exceptions to this rule are the _ignored and _prefix completers.
The default value for this style is _complete _ignored, i.e. normally only completion will be done, first using the ignored-patterns style and the $fignore array and then without ignoring matches.
This can be useful on systems that support special filesystems whose top-level pathnames can not be listed or generated with glob patterns. It can also be used for directories for which one does not have read permission.
If the file-patterns style is set, the default tags are not used. Instead, the value of the style says which tags and which patterns are to be offered. The strings in the value contain specifications of the form `pattern:tag'; each string may contain any number of such specifications. The pattern gives a glob pattern that is to be used to generate filenames. If it contains the sequence `%p', that is replaced by the pattern(s) given by the calling function. Colons in the pattern must be preceded by a backslash to make them distinguishable from the colon before the tag. If more than one pattern is needed, the patterns can be given inside braces, separated by commas. The tags of all strings in the value will be offered by _files (again, one after another) and used when looking up other styles. For strings containing more than one specification, the filenames for all specifications will be generated at the same try. If no `:tag' is given the `files' tag will be used. The tag may also be followed by an optional second colon and a description. If that is given, this description will be used for the `%d' in the value of the format style (if that is set) instead of the default description supplied by the completion function. If the description given here contains itself a `%d', that is replaced with the description supplied by the completion function.
For example, to make the rm command first complete only names of object files and the names of all files if no object file matches the string on the line, one would do:
zstyle ':completion:*:*:rm:*' file-patterns \ '*.o:object-files' '%p:all-files' |
Another interesting example is to change the default behaviour that makes completion first offer files matching the patterns given by the calling function, then directories and then all files. Many people prefer to get both the files matching the given patterns and the directories in the first try and all files at the second try. To achieve this, one could do:
zstyle ':completion:*' file-patterns \ '%p:globbed-files *(-/):directories' '*:all-files' |
This works even for contexts in which all files would be completed, because _files will not try a pattern more than once and it stops when the pattern `*' was tried.
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.
This style is tested for the default tag and all tags used when generating matches. This allows one to turn unconditional listing on for certain types of matches.
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 provides the possibility of defining different format strings for different types of matches.
Note also that some completer functions define additional `%'-sequences. These are described for the completer functions that make use of them.
For the messages tag, this style defines a string used by some completion functions to display messages. Here, the `%d' is replaced with a message given by the completion function.
Finally, when set with the warnings tag, the format string 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 separated by spaces and the sequence `%D' is replaced with those descriptions separated by newlines.
The `%' for the sequences that are replaced by strings provided by the completion functions like the `%d' may be followed by field width specifications as described for the zformat builtin command from the zsh/zutil module, see 21.23 The zsh/zutil Module.
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-.
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 |
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.
The values `current' and `current-shown' are a bit like the opposite of accept-exact. They mean that only strings with missing characters will be completed.
Note that you almost certainly don't want to set this to `true' or `other' for a general context such as `:completion:*'. This is because it would disallow completion of, for example, options multiple times even if the command in question accepts the option more than once.
In addition, the value may include one or both of:
Note that names of directories ignored because of one of the tests will be ignored in the same way as the matches ignored because of the ignored-patterns style. I.e., by using the _ignored completer it is possible to complete these directories nonetheless.
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.
The value may also contain the substrings `pending' or `pending=val' to make the character typed to start completion be inserted instead of completion being tried when there is input pending which has not yet been processed by the shell. If a val is given, completion will not be done if there are at least that many characters of unprocessed input. This is often useful to have set when pasting characters into a terminal. Note however, that it relies on the $PENDING special parameter from the zsh/zle module being set properly which is not guaranteed on all platforms.
The default value of this style is `true' unless when completing inside the vared builtin command, where it defaults to `false'.
When used for the _match completer, the style may also be set to the string `pattern'. This makes the pattern on the line be left unchanged if it didn't match unambiguously.
Note that with one of the `true' values, the _expand completer returns if there is only one expansion and that is, after restoring the original prefix, the same as the original word. This means that other completers will be called immediately after _expand.
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. For this to work best, the group-name style must be set to an empty string. If the group-name tag specifies other names for the groups the matches in these groups can be colored by using these names together with the `(group)...' syntax described for the ZLS_COLORS and ZLS_COLOURS parameters and adding the specifications to the value for this style with the default tag (although in most cases it should work by setting this style for the appropriate tags).
It is possible to use the same specifications set up for the GNU version of the ls command:
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} |
The default colors are the same as for the GNU ls command and can be obtained by setting the style to an empty string (i.e. ").
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' |
By default every specification replaces previous ones. If specification is prefixed with +, it is added to the existing list. This allows testing more general patterns without repeating the whole list every time, as in:
zstyle ':completion:*' matcher-list '' '+m{a-Z}={A-Z}' '+m{A-Z}={a-z}' |
The style allows even finer control by specifying a particular completer, without the leading underscore, in the third field of the completion context. For example, if one uses the completers _complete and _prefix but wants to try case-insensitive completion only when using the _complete completer, one would do:
zstyle ':completion:*' completer _complete _prefix zstyle ':completion:*:complete:*' matcher-list \ '' 'm:{a-zA-Z}={A-Za-z}' |
Note that the completer style allows user-defined names to be used in the context instead of the name of the completer. This is useful if, for example, one wants to try normal completion without a match specification and with case-insensitive matching first, correction if that doesn't generate any matches and partial-word completion if that doesn't yield any matches either. In this case one can give the _complete completer more than once in the completer style and define different match specifications for each occurrence, as in:
zstyle ':completion:*' completer _complete _correct _complete:foo zstyle ':completion:*:complete:*' matcher-list \ '' 'm:{a-zA-Z}={A-Za-z}' zstyle ':completion:*:foo:*' matcher-list \ 'm:{a-zA-Z}={A-Za-z} r:|[-_./]=* r:|=*' |
If the style is unset in any context no match specification is applied; further, some completers such as _correct and _approximate do not use the match specifications at all. However, it is always safe to use the simple form for this style (as in the first example above), since any completers which do not use match specifications will only ever be called once, rather than once per specification.
Since the specification-strings in this style have to be tried one after another, it is a good idea to keep their number low. In most cases one to three strings (each of which may, without to large a performance hit, consist of more than one single match specification) will give acceptable performance.
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:::' max-errors 2 numeric |
two errors are allowed if no numeric argument is given, but 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.
The default value for this style is `2 numeric'.
Using the form `yes=num', where `yes' may be any of the true values (`yes', `true', `on' and `1') turns on menu completion if there at least num matches. Using this for one of the `false' values (as in `no=10') makes menu completion not be used if there are num or more matches. Of course, this is only useful when menu completion is normally used, e.g. by setting the MENU_COMPLETE option. The `true' values may also be used in the form `yes=long' to turn on menu completion if the list does not fit onto the screen. This will start menu completion only if normal completion was attempted, not when only the list of possible completions was requested. To start menu completion even then, the value `yes=long-list' can be used.
In addition to (or instead of) the above possibilities, the value may contain the string `select', optionally followed by an equals sign and a number. In this case menu selection (as defined by the zsh/complist 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 provide a number, the smallest number is taken. Menu selection can be turned off explicitly by defining a value containing the string `no-select'.
It is also possible to start menu selection only if the list of matches does not fit on the screen by using the value `select=long'. This will only start menu selection if the widget invoked does completion, not simply listing as done by delete-char-or-list; to start menu selection even here, use the value `select=long-list'.
To turn on menu completion or menu selection when a certain number of matches is generated or the list of matches does not fit onto the screen, both of `yes=' and `select=' can be given twice, once with a number and once with `long' or `long-list'.
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 a common prefix string, if there is any. However, this may remove parts of the original pattern, so that further completion could produce more matches than on the first attempt. By using the _oldlist completer and setting this style 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, or with this style set to false, typing TAB at this point would start trying to complete the line as it now appears. With _oldlist, it instead continues to cycle through the list of corrections.
zstyle ':completion:*:complete:dpkg:option--status-1:*' \ packageset avail |
causes available packages, rather than only installed packages, to be completed for `dpkg --status'.
The default value for this style is `false'.
The default style for this style is `true'.
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).
The default for this style is `false'.
The default is `true'.
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. (See the file-patterns style for an exception to this behavior.)
For example:
zstyle ':completion:*:complete:-command-:*' tag-order \ 'commands functions' |
specifies that completion in command position should offer only completions for external commands and shell functions immediately.
In addition to tag names, each string in the value may take one of the following forms:
The label may optionally be followed by a second colon and a description. This description will then be used for the `%d' in the value of the format style instead of the default description supplied by the completion function. Spaces in the description have to be quoted by preceding them with a backslash and a `%d' appearing in the description is replaced with the description given by the completion function.
In each of the cases above, the tag may also be a pattern or more than one pattern inside braces and separated by commas. In this case all of the offered tags matching the pattern(s) will be used except for those that are given explicitly in the same string. There are probably two main uses of this. One is the case where one wants to try one of the tags more than once, setting other styles differently for each try, but still wants to use all the other tags without having to repeat them all. For example, to make completion of function names in command position ignore all the completion functions starting with an underscore the first time completion is tried, one could do:
zstyle ':completion:*:*:-command-:*' tag-order \ 'functions:-non-comp *' functions zstyle ':completion:*:functions-non-comp' ignored-patterns '_*' |
Here, the completion system will first try all tags offered, but will use the tag label functions-non-comp when looking up styles for the function names completed. For this, the ignored-patterns style is set to exclude functions starting with an underscore from the set of possible matches. If none of the generated matches match the string on the line, the completion system will use the second value of the tag-order style and complete functions names again, but this time using the name functions to look up styles, so that the ignored-patterns style is not used and all function names are considered.
Of course, this can also be used to split the matches for one tag into different groups. For example:
zstyle ':completion:*' tag-order \ 'options:-long:long\ options options:-short:short\ options options:-single-letter:single\ letter\ options' zstyle ':completion:*:options-long' ignored-patterns '[-+](|-|[^-]*)' zstyle ':completion:*:options-short' ignored-patterns '--*' '[-+]?' zstyle ':completion:*:options-single-letter' ignored-patterns '???*' |
With the group-names style set, this makes options beginning with `--', options beginning with a single `-' or `+' but containing multiple characters, and single-letter options be displayed in separate groups with different descriptions.
The second interesting use of patterns is the case where one wants to try multiple match specifications one after another. The matcher-list style offers something similar, but it is tested very early in the completion system and hence can't be set for single commands nor for more specific contexts. Here is how to try normal completion without any match specification and, if that generates no matches, try again with case-insensitive matching, restricting the effect to arguments of the command foo:
zstyle ':completion:*:*:foo:*' tag-order '*' '*:-case' zstyle ':completion:*-case' matcher 'm:{a-z}={A-Z}' |
First, all the tags offered when completing after foo are tried using the normal tag name. If that generates no matches, the second value of tag-order is used, which tries all tags again except that this time each has -case appended to its name for lookup of styles. Hence this time the value for the matcher style from the second call to zstyle in the example is used to make completion case-insensitive.
Using the -e option of the zstyle builtin command, it is possible to specify conditions saying when certain tags are to be used. For example:
zstyle -e '*:-command-:*' tag-order ' if [[ -n $PREFIX ]]; then reply=( ) else reply=( - ) fi' |
Makes completion in command position happen only if the string on the line is not empty. This is tested using the PREFIX parameter which is special in completion widgets; see 18. Completion Widgets for a description of these special parameters. Setting reply to an empty array ensures that only the default behaviour of trying all tags at once is used and setting it to an array containing only a hyphen disables that default behaviour -- thus keeping all tags from being tried.
If no style has been defined for a context, the strings `(|*-)argument-* (|*-)option-* values' and `options' plus all tags offered by the completion function will be used to provide a sensible default behavior that causes arguments (whether normal command arguments or arguments of options) to be completed before option names for most commands.
If the value contains only one string and that is the name of a normal file, the URLs are taken from that file (where the URLs may be separated by white space or newlines).
Finally, if the only string in the value names a directory, that should contain sub-directories named after the retrieval methods which occur as the first part of a URL, i.e. `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.
Note that this is only intended to smooth the transition from compctl to the new completion system and may disappear in the future.
Note also that the definitions from compctl will only be used if there is no specific completion function for the command in question. For example, while completing arguments to the command foo, if this was handled by a command function _foo, compctl would never be tried, while if it was handled by _default, compctl would be tried.
If set for the my-accounts tag, this is used for commands such as rlogin and ssh; in this case the style should contain the names of the user's own accounts on remote hosts. If set for the other-accounts tag, it is used for commands such as talk and finger and should contain other people's accounts. Finally, it may also be used by some commands with the accounts tag.
The default value for this style is `true'.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 is `_complete _ignored', i.e. normally only ordinary completion is tried, first with the effect of the ignored-patterns style and then without it. 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.
If the first argument to _main_complete is a single hyphen, the arguments will not be taken as names of completers. Instead, the second argument gives a name to use in the completer field of the context and the other arguments give a command name and arguments to call to generate the matches.
The following completer functions are contained in the distribution (users may write their own):
This function also uses the style old-matches. If it is set to `true' or to the string `only' and there is a list of matches from a previous completion, those matches will be inserted in the command line. If it is set to the the string `only', it will only insert an old list and won't add the string for all matches of the list currently being generated.
With the old-matches style set, this completer should probably not be called unconditionally. Instead one could use the -e option of the zstyle builtin command to add a condition to the completer or to the old-matches style. Alternatively, one could use the _generic function to bind _all_matches to a separate key binding, for example:
zle -C all-matches complete-word _generic bindkey '^Xa' all-matches zstyle ':completion:all-matches:*' old-matches only zstyle ':completion:all-matches:*' completer _all_matches |
zstyle ':completion:*' completer _complete _approximate |
This will give correcting completion if and only if normal completion yields no possible completions. When corrected completions are found, the completer will normally start menu completion allowing you to cycle through these strings.
This completer uses the tags corrections and original when generating the possible corrections 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.
As with all completers, _approximate uses its name without the underscore in the completer field of the context name. Once it has started trying to generate matches, it will append a minus sign and the number of errors accepted to its name. _approximate will first look for completions with one error, then two, and on so up to the limit on the number of errors set by the max-errors style. Hence on the first try the completer field of the context contains `approximate-1', on the second try `approximate-2', and so on.
When _approximate is called from another function, the number of errors to accept may be given with the -a option. Its argument should be the same as the value of the max-errors style, all in one string.
Note that this completer (and the _correct completer mentioned below) can be quite expensive to call, especially when a large number of errors are allowed. One way to avoid this is to set up the completer style using the -e option to zstyle so that some completers are only used when completion is attempted a second time on the same string, e.g.:
zstyle ':completion:*' completer ' if [[ $_last_try != "$HISTNO$BUFFER$CURSOR" ]]; then _last_try="$HISTNO$BUFFER$CURSOR" reply=(_complete _match _prefix) else reply=(_ignored _correct _approximate) fi' |
This uses the HISTNO parameter and the BUFFER and CURSOR special parameters that are available inside zle and completion widgets to find out if the command line hasn't changed since the last time completion was tried. Only then are the _ignored, _correct and _approximate completers called.
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. If it is set to an array, the elements are taken to be the possible matches which will be completed using the tag `values' and the description `value'. If it is set to an associative array, the keys are used as the possible completions and the values (if non-empty) are used as descriptions for the matches. If `compcontext' is set to a string containing colons, it should be of the form `tag:descr:action'. In this case the tag and descr give the tag and description to use and the action says what should be completed in one of the forms described for the _arguments utility function below.
Finally, if `compcontext' is set to a string without colons, the value is taken as the name of the context to use and the function defined for that context will be called. For this purpose, there is a special context named -command-line- that completes whole command lines (commands and their arguments) and is not used by the completion system itself, but has a function handling completion for it.
For example, with:
zstyle ':completion:::::' completer _complete _correct _approximate zstyle ':completion:*:correct:::' max-errors 2 not-numeric zstyle ':completion:*:approximate:::' max-errors 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.
When _correct is called as a function, the number of errors to accept may be given following the -a option. The argument should be the same as the value of the accept style, all in one string.
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.
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.
Which kind of expansion is tried is controlled by the substitute, glob and subst-globs-only styles.
When _expand is called as a function, the different modes may be selected with options. The -s to substitute, -g to glob and -o to subst-globs-only.
This function is also a bindable command, see 19.5 Bindable Commands.
The single-ignored style is used if only one match could be generated. It can be set to show to prevent that match from being displayed or inserted into the line, or it can be set to menu, in which case the single match and the original string from the line will be offered in a menu completion.
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 match-original style 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 menu completion unless the insert-unambiguous style is set to `true'. In this case menu completion will only be started if no unambiguous string could be generated that is at least as long as the original string. The style may also be set to the string `pattern'. This will keep the pattern on the line intact as long as there isn't an unambiguous completion with which it could be replaced.
Note that the matcher specifications defined globally or used by the completion functions will not be used.
For example, consider this global completer style:
zstyle ':completion:*' completer \ _complete _prefix _correct _prefix:foo |
Here, the _prefix completer tries normal completion but ignoring the suffix. If that doesn't generate any matches, and neither does the call to the _correct completer after it, _prefix will be called a second time and, now only trying correction with the suffix ignored. If you want to use _prefix as the last resort and try only normal completion, you can use:
zstyle ':completion:*' completer _complete ... _prefix zstyle ':completion::prefix:*' completer _complete |
The add-space style is also used. If it is set to `true' then _prefix will insert a space between the matches generated (if any) and the suffix.
Note that this completer is only useful if the COMPLETE_IN_WORD option is set; otherwise, the cursor will be moved to the end of the current word before the completion code is called and hence there will be no suffix.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
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.
When used as a bindable command there is one additional feature that can be selected by setting the complete style to `true'. In this case, if the word isn't the name of an alias, _expand_alias tries to complete the word to a full alias name without expanding it (but leaving the cursor directly after the completed word so that invoking _expand_alias once more will expand the now-complete alias name).
zle -C foo complete-word _generic bindkey '...' foo zstyle ':completion:foo:*' menu yes select=1 |
Second, 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.
Note that the information about styles may be incomplete; it depends on the information available from the completion functions called, which in turn is determined by the user's own styles and other settings.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Descriptions follow for utility functions that may be useful when writing completion functions. Most of these reside in the Base subdirectory. 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 writing completion functions or other ZLE widgets that call completion, it might be interesting to know about two more features offered by the _main_complete function. The arrays compprefuncs and comppostfuncs may be set to contain names of functions that are to be called immediately before or after completion has been tried. The functions will only be called once, unless they put themselves into the arrays again.
For example:
local expl ... if _requested foo; then ... _all_labels foo expl '...' compadd ... - $matches fi |
Will complete the strings from the matches parameter, using compadd with additional options which will take precedence over those generated by _all_labels.
The tags to use and the action to perform if a tag is 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 description descr. The actions supported are those used by the _arguments function (described below), without the `->state' and `=...' forms.
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).
Note that, like _arguments this will also use _all_labels to execute the actions, so one doesn't need to call that explicitly unless another tag is to be used, for example in a function called from _alternative.
Like _tags this function supports the -C option to give a different name for the argument context field.
With two colons before the message, the words special array and the CURRENT special parameter are modified to refer only to the normal arguments when the action is executed or evaluated. With three colons before the message they are modified to refer only to the normal arguments covered by this description.
In the descriptions below, the option names represented by optname are normally taken to be multi-character names, and a word from the line is considered to contain only one option (or none). By giving the -s option to _arguments before the first spec, each optname is considered to be a single character and each word from the line may contain more than one such option letter. However, words 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.
The -s option may be combined with the option -w to say that more option characters are to be expected even after an option that takes an argument. For example, if a command takes the options `a' and `b', where `a' takes an argument in the next word, _arguments would normally not complete the other option directly after `-a', but it would allow that if given the -w option.
Similarly, the option -W may be given together with -s to force completion of single-letter options even after options that get an argument in the same word. For example, if a command takes the options `a' and `b', where `a' needs an argument in the same word, directly after the option character, _arguments would normally only execute the action for that argument and not offer other single-letter options as possible completions. If given the -W option, it will offer other options as possible completions after executing the action for the argument. Note that, depending on the action, this may mean that the other options can't really be completed, but at least they will be listed. For more control, use an utility function like _guard in the argument's action.
The forms of optspec are:
If the command accepts the option with either a leading minus or a leading plus sign, use either `-+optname' or `+-optname' to define both variants at once.
In all the following forms, the leading `-' may be replaced or paired with `+' in this way.
The verbose style is used to decide if these explanation strings should be displayed with the option in a completion listing.
If no bracketed explanation string is given but the auto-description style is set and only one argument is described for this optspec, the value of the style is displayed, with any appearance of the sequence `%d' in it replaced by the message of the first description that follows the optspec; see below.
Note that the special meaning of a leading or trailing - or + in optspec means that when the command to be completed accepts options like `-+' or `-=', the second character has to be quoted with a backslash, as in `-\+'.
Each description following an optspec must take one of the following forms:
When the message is preceded by two colons, the words special array and the CURRENT special parameter are modified during the execution or evaluation of the action to refer only to the words after the option. When preceded by three colons, they are modified to refer only to the words covered by this description.
Note that only one such `:*'-specification is useful and no other argument specification may be given after it.
To include a colon in any optname, message, or action anywhere above, it has to be preceded by a backslash, as `\:'.
Each of the six forms of spec (yes, there are six, keep track of the nestings) may be preceded by a list of option names and argument numbers with which the option or argument described is mutually exclusive. This list is given in parentheses, as in `(-two -three 1)-one:...' or `(-foo):...'. In the first example, the options `-two' and `-three' and the first argument will not be offered as possible completions if the option `-one' is on the line before the cursor, and in the second example the option `-foo' will not be offered if the argument described by the specification is on the line.
The list may also contain a single star (*) as one of its elements to specify that the description for the rest arguments (i.e. a specification of the form `*:...') should not be used, a colon (:) to specify that the descriptions for all normal (non-option-) arguments should not be used and a hyphen (-) to specify that the descriptions for all options should not be used. This paragraph desperately needs rewriting.
To simplify writing writing functions that call _arguments more than once, the specs may also start with the character `!' (exclamation mark) to make the spec not be completed. However, if this is used with one of the forms describing options, the option (and its arguments, if it takes any) will be understood and skipped if they appear on the command line. It's just that the option itself will not be completed. This is intended to be used with an array containing the options used in the first call to arguments. The second call can then use `\!${^global_options}' to ignore those options and complete only the ones understood in the current context.
In every case above, the action determines how the possible completions should be generated. 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.
Except for the `->string' form below, the action will be executed by calling the _all_labels function to process all tag labels, so one doesn't need to call that explicitly unless another tag is to be used, for example in a function called in the action.
When only one of a fixed set of strings can be completed, the action can consist of these strings as a list in parentheses, as in:
:foo:(foo bar baz) |
Such a list in doubled parentheses should contain strings consisting of the string to complete followed by `\:' and a description, as in:
:foo:((a\:bar b\:baz)) |
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's (with all leading and trailing spaces and tabs removed) of all actions that have to be used will be stored in the global array state. The function returns with a non-zero return value if the cursor is not in a position where options can be completed or if the current word could not be completed to an option. But if the -R option is given to _arguments, the function will instead return 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. This enables wrapper functions around _arguments to be able to find out if they have to make sure that the special completion parameters are not reset when they return.
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 is evaluated to generate the matches and if the action does not begin with an opening parentheses or brace, it is also 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[@]').
If the action starts with `= ' (an equals sign followed by a space), _arguments will insert the contents of the argument field of the current context as the new first element in the words special array and increments the value of the CURRENT special parameter. In other words, it inserts a dummy element in the words array and makes CURRENT still point to the word in that array where the cursor is. This is only really useful when used with one of the forms that make _arguments modify the words array to contain only some of the words from the line, i.e. one of the argument description forms where the message is preceded by two or three colons. For example, when the function called in the action for such an argument itself uses _arguments, the dummy element is needed to make that second call to _arguments use all words from the restricted range for argument parsing. Without the inserted dummy element, the first word in the range would be taken (by the second _arguments) to be the command name and hence ignored.
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' (set only in the calling function when using an action of the form `->string', not during the evaluation of other actions) is set to the automatically created context names. These are either strings of the form `option-opt-n' for the n'th argument of the option -opt, or strings 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 `option-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 is changed by appending the same string that is stored in the context parameter.
It is also possible to specify multiple sets of options and arguments with the sets separated by single hyphens. The specifications before the first hyphen are shared by all sets given after the first hyphen. The first word in every other set gives the name of the set. This name may appear in exclusion lists in the specifications, either alone or before one of the possible values described above (with a `-' between the name and the rest).
For example:
_arguments \ -a \ - set1 \ -c \ - set2 \ -d \ ':arg:(x2 y2)' |
This defines two sets. When the command line contains the option `-c', the `-d' option and the argument will not be considered possible completions. When it contains `-d' or an argument, the option `-c' will not be completed any more, but if `-a' is given, both sets will still be considered valid, because it appears before the first hyphen, so both sets contain this option.
If the name-string is of the form `(name)' then all specifications in the set have an implicit exclusion list containing the name of the set, i.e. all specifications are mutual exclusive with all other specifications in the same set. This is useful for defining multiple sets of options which are mutually exclusive and in which the options are aliases for each other. E.g.:
_arguments \ -a -b \ - '(compress)' \ {-c,--compress}'[compress]' \ - '(uncompress)' \ {-d,--decompress}'[decompress]' |
Note that using multiple sets will be slower than using only one set because the completion code has to parse the command line once for every set. So more than one set should only be used if the command syntax is too complicated. Note also that an option specification with rest-arguments (as in `-foo:*:...') often allows the use of multiple sets to be avoided.
To simplify the specifications for commands with standard option parsing, the options -S and -A may be given. With -S, no option will be completed after a `--' on the line and this argument will otherwise be ignored. With -A, no options will be completed after the first non-option argument on the line. The -A has to be followed by a pattern matching all strings which are not to be taken as arguments. For example, to make _arguments stop completing options after the first normal argument, but ignoring all strings starting with a hyphen even if they are not described by one of the optspecs, one would use: `-A "-*"'.
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 an 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 an `=', 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.
If the pattern ends in `(-)', this will removed from the pattern and the action will be used only directly after the `=', not in the next word. I.e., this is like a normal specification as described above using `=-'.
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.
Example:
_example_caching_policy () { # rebuild if cache is more than a week old oldp=( "$1"(Nmw+1) ) (( $#oldp )) } |
In any case, the strings from the call to _call_program or from the style are concatenated with spaces between them and the resulting string is evaluated. The return value is the return value of the command called.
The style name should consist of multiple parts separated by hyphens which are then used as field names. Known values for such fields can be given after the second argument in arguments of the form `field=pattern'. The first argument without an equals 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 characters matching the pattern given after the -s option to _combination; normally this is used to define character classes like the `-s "[:@]"' used for the users-hosts style.
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 field name are passed to compadd when generating matches from the style value, or to the functions for the fields if they are called.
The descr is taken as a string to display above the matches if the format style for the descriptions tag is set. After this come one or two names of arrays followed by options to pass to compadd. The first array contains the possible completions with their descriptions in the form `completion:description'. If a second array is given, it should have the same number of elements as the first one and the corresponding elements are added as possible completions instead of the completion strings from the first array. The completion list will retain the descriptions from the first array. Finally, a set of completion options can appear.
If the option `-o' appears before the first argument, the matches added will be treated as option names (typically following a `-', `--' or `+' on the command line). This makes _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.
_describe uses the _all_labels function to generate the matches, so it does not need to appear inside a loop over tag labels.
The string returned by the format style (if any) will be modified so that the sequence `%d' is replaced by the descr given as the third argument without any leading or trailing white space. If, after removing the white space, the descr is the empty string, the format style will not be used and the options put into the name array will not contain an explanation string to be displayed above the matches. If _description is called with more than three arguments, the additional 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 an 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, the function will be used like this:
local expl _description files expl file compadd "$expl[@]" - "$files[@]" |
Note the use of the parameter expl, the hyphen, and the list of matches. Almost all calls to compadd within the completion system use a similar format; this ensures that user-specified styles are correctly passed down to the builtins which implement the internals of completion.
See _path_files below for a description of the full set of options accepted by _files.
compdef _gnu_generic foo bar |
in one of the initialization files after the call to compinit.
The default installation uses this function only to generate completions for some GNU-commands because to complete the options, the command has to be called and hence it shouldn't be used if one can't be sure that the command understands the `--help' option.
The pattern may be preceded by those options understood by compadd that are passed down from _description, namely -M, -J, -V, -1, -2, -n, -F and -X. All of these options, except -X, will be ignored. If the -X option appears, the description following it will be used as the string to display if the pattern matches, unless the option descr is given to _guard itself, which will then take precedence.
As an example, consider a command taking the options -n and -none, where -n has to be followed by a numeric value in the same word. By using either of:
_argument '-n-:numeric value:_guard "[0-9]#"' '-none' |
or
_argument '-n-: :_guard "[0-9]#" "numeric value"' '-none' |
_arguments can be made to both display the message `numeric value' and complete options after `-n<TAB>'. If the `-n' is already followed by one or more digits (matching the pattern given to _guard), only the message will be displayed and if the `-n' is followed by another character, only options are completed.
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.
If the -r option is given, no style is used and the descr is used literally as the string to display. This is only used in cases where that string is taken from some pre-processed argument list containing an expanded description.
If the -i option is present, then any time there is a unique match it will immediately be inserted even if that requires additional separators to be inserted as well. When completing from a fixed set of possible completions which are really words, this is often the expected behaviour; however, if _multi_parts should behave like completing pathnames, the -i option should not be used.
Like other utility functions, this function accepts the `-V', `-J', `-1', `-2', `-n', `-f', `-X', `-M', `-P', `-S', `-r', `-R', and `-q' options and passes them to the compadd builtin.
The -12JV options and the first three arguments are given to the _description function using the tag label instead of the first argument as appropriate. The options given after the descr should be other options to be used for compadd or whatever function is to be called to add the matches. _next_label will store these options in the parameter whose name is given as the second argument. This is done in such a way that the description given by the user to the tag-order style is preferred over the one given to _next_label.
Note that this function must not be called without a previous call to _tags or _requested because it uses the tag label for the current tag found by these functions.
A normal use of this function for the tag labels of the tag foo looks like this:
local expl ret=1 ... if _requested foo; then ... while _next_label foo expl '...'; do compadd "$expl[@]" ... && ret=0 done ... fi return ret |
The function can also be called by other completion functions which need to treat a range of words as a command line. 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 _normal again, with the effect that `nohup cmd ...' is treated the same way was `cmd ...'.
If the command name matches a pattern, the parameter _compskip is checked after the call to the corresponding completion function. This has the same effect here as in the -first- context: if it is set, no more completion functions are called even if there are no matches so far.
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. Hence these options are not generally used by the completion system.
The options accepted by both _path_files and _files are:
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, ambiguous, special-dirs, list-suffixes and file-sort.
Specification arguments take one of following forms, in which metacharacters such as `(', `)', `#' and `|' should be quoted.
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 actions for the target are executed. In this case, nothing is actually removed from the command line string so that any previous or neighbouring state may also have actionss. actionss evaluation are ordered by the tag-order style and specified tag by _alternative. So, the various formats supported by _alternative can be used in action. descr is used for setting up the array parameter expl.
_tags foo bar baz while _tags; do if _requested foo; then ... # perform completion for foo fi ... # test the tags bar and baz in the same way ... # exit loop if matches were generated done |
Note that the test for whether matches were generated is not performed until the end of the _tags loop. This is so that the user can specify a set of tags to be tested at the same time in the tag-order parameter.
If the name and the descr are given, _requested calls the _description function with these arguments, including the options.
If the command is given, the _all_labels function will be called immediately with the same arguments. This is often useful to do both the testing of the tag, getting the description for the matches and adding the matches at once. For example:
local expl ret=1 _tags foo bar baz while _tags; do _requested foo expl 'description' \ compadd foobar foobaz && ret=0 ... (( ret )) || break done |
Note that this means that the command has to accept the options that have to be passed down to compadd.
See _store_cache below for more details.
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.
The optional group gives the name of the group in which the matches will be placed. If it is not given, the tag is used as the group name.
Note that this function is called automatically from _description so that one normally doesn't have to call it explicitly.
The cache_identifier specifies the file which the data should be dumped to, and is stored in a directory specified by the cache-path style (defaults to ~/.zsh/cache). The remaining vars arguments are the variables to dump to the file.
The return value is zero if storage was successful. The function will only attempt storage if the use-cache style is set, so you can call this function without worrying about whether the user wanted to use the caching layer.
If your completion function avoids calling _retrieve_cache when it already has the completion data in the environment, it should probably at least call _cache_invalid to check whether this data and the data cached on disk is still valid.
See the _perl_modules completion function for a simple example of usage of this caching layer.
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) stored in the argument field of the context name in the curcontext parameter. This allows _tags to be made to use a more specific context name without having to change and reset the curcontext parameter (which would otherwise have the same effect).
If the first argument is the option `-O name', this will be used in the same way as by the _arguments function, in other words 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. Thus the values completed appear in the same word on the command line, unlike completion using _arguments.
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 differences are that no minus or plus sign is required at the beginning, that values can have only one argument and that those forms of actions beginning with an equal sign are not supported.
The character separating a value from its argument can be set using the option -S (like -s, followed by the character to use as the separator in the next argument). If this option is not used, the equal sign will be used as the separator.
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 is described as `bar', takes no argument and may appear more than once. The second is described as `number', may appear more than once, and takes 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 takes an optional argument described 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 style verbose is used.
One last difference from _arguments is that this function uses the associative array val_args to report values and their arguments, although 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.
Note also that _values normally adds the character used as the separator between values as a auto-removable suffix so that users don't have to type it themselves. But when using a `->string' action _values can't do that because the matches for the argument will be generated by the calling function. To get the usual behaviour, the implementor of the calling function has to add the suffix directly by passing the options `-qS x' (where x is the separator character specified with the -s option of _values) to the function generating the matches or to the compadd builtin.
Like _arguments, _values supports the -C option in which case you have to make the parameter curcontext local instead of context (as described above).
Like _requested, it should be passed arguments as for _description. It calls _tags with the given tag and if that returns zero (so that the tag is requested by the user) it calls _description. Hence to offer only one tag and immediately use the description generated:
_wanted tag expl 'description' \ compadd matches... |
Unlike _requested, however, _wanted cannot be called without the command. This is because _wanted also implements the loop over the tags, not just the one for the labels; conversely, it should not be called in the middle of a _tags loop.
Note that, as for _requested, the command has to accept the options that have to be passed down to compadd.
Like _tags this function supports the -C option to give a different name for the argument context field.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |