[Contents]   [Back]   [Prev]   [Up]   [Next]   [Forward]  


Completion Widgets

Description

Completion widgets are defined by the -C option to the zle builtin command provided by the zle module (see section The zle Module). For example,

zle -C complete expand-or-complete completer

defines a widget named complete. When this widget is bound to a key using the bindkey builtin command defined in the zle module (see section Zsh Line Editor), typing that key will call the shell function completer. This function is responsible for generating the possible matches using the builtins described below. Once the function returns, the completion code takes over control again and treats the matches as the builtin widget expand-or-complete would do. For this second argument, the name of any of the builtin widgets that handle completions can be given: complete-word, expand-or-complete, expand-or-complete-prefix, menu-complete, menu-expand-or-complete, reverse-menu-complete, list-choices, or delete-char-or-list. Note that this will still work even if the widget in question has been rebound.

Special Parameters

Inside completion widgets, and any functions called from those, some parameters have special meaning; outside these function they are not special to the shell in any way. These parameters are used to pass information between the completion code and the completion widget. Some of the builtin commands and the condition codes use or change the current values of these parameters. Any existing values will be hidden during execution of completion widgets; except for compstate, the parameters are reset on each function exit (including nested function calls from within the completion widget) to the values they had when the function was entered.

words
This array contains the words present on the command line currently being edited.
CURRENT
This is the number of the current word, i.e. the word the cursor is currently on in the words array. Note that this value is only correct if the ksharrays options is not set.
PREFIX
Initially this will be set to the part of the current word from the beginning of the word up to the position of the cursor; it may be altered to give a common prefix for all matches.
IPREFIX
Initially this will be set to the empty string. It functions like PREFIX, and gives a string which precedes the one in PREFIX and is not considered part of the list of matches. Typically, a string is transferred from the beginning of PREFIX to the end of IPREFIX, for example:

IPREFIX=${PREFIX%%\=*}=
PREFIX=${PREFIX#*=}

causes the part of the prefix up to and including the first equal sign not to be treated as part of a matched string. This can be done automatically by the compset builtin, see below.

QIPREFIX
This parameter is read-only and contains the quoted string up to the word being completed. E.g. when completing `"foo', this parameter contains the double quote. If the -q option of compset is used (see below), and the original string was `"foo bar' with the cursor on the `bar', this parameter contains `"foo '.
SUFFIX
Initially this will be set to the part of the current word from the cursor position to the end; it may be altered to give a common suffix for all matches. It is most useful when the option COMPLETE_IN_WORD is set, as otherwise the whole word on the command line is treated as a prefix.
ISUFFIX
As IPREFIX, but for a suffix that should not be considered part of the matches; note that the ISUFFIX string follows the SUFFIX string.
QISUFFIX
Like QIPREFIX, but containing the suffix.
compstate
This is an associative array with various keys and values that the completion code uses to exchange information with the completion widget. The keys are:

context
This will be set by the completion code to the overall context in which completion is attempted. Possible values are:

command
when completing for a normal command (either in a command position or for an argument of the command).
redirect
when completing after a redirection operator.
condition
when completing inside a `[[...]]' conditional expression; in this case the words array contains the words inside the conditional expression.
math
when completing in a mathematical environment such as a `((...))' construct.
value
when completing the value of a parameter assignment.
array_value
when completing inside the value of an array parameter assignment; in this case the words array contains the words inside the parentheses.
subscript
when completing inside a parameter subscript.
parameter
when completing the name of a parameter in a parameter expansion beginning with $ but not ${.
brace_parameter
when completing the name of a parameter in a parameter expansion beginning with ${.
vared
If completion is called while editing a line using the vared builtin, the value of this key is set to the name of the parameter given as argument to vared. If vared is not currently used, this key is unset.
parameter
The name of the parameter when completing in a subscript or in the value of a parameter assignment.
redirect
The redirection operator when completing in a redirection position, i.e. one of <, >, etc.
quoting
When completing inside single quotes, this is set to the string single; inside double quotes, the string double; inside backticks, the string backtick. Otherwise it is unset.
quote
When completing inside quotes, this contains the quotation character (i.e. either a single quote, a double quote, or a backtick). Otherwise it is unset.
nmatches
The number of matches generated and accepted by the completion code so far.
normal_nmatches
Like nmatches, but counts only matches in the normal set. I.e. file names with one of the suffixes from the fignore array and matches put into the alternate set using the -a option of the compadd builtin command (see below) are not counted.
matcher
When completion is performed with a global match specification as defined by

compctl -M spec1 ... specN ...

this gives the number of the specification string currently in use. In this case, matching is performed with each specification in turn.

matcher_string
The global match specification string specN currently used.
total_matchers
The total number of global match specifications.
restore
This is set to auto before a function is entered, which forces the special parameters mentioned above (words, CURRENT, PREFIX, IPREFIX, SUFFIX, and ISUFFIX) to be restored to their previous values when the function exits. If a function unsets it or sets it to any other string, they will not be restored.
list
This controls whether or how the list of matches will be displayed. If it is unset or empty they will never be listed; if is set to list, they will always be listed; if autolist or ambiguous, they will be listed when the AUTO_LIST or LIST_AMBIGUOUS options respectively would normally cause them to be. It will be set appropriately on entry to a completion widget and may be changed there.
force_list
If the value for the list key is autolist or ambiguous, the list will normally be shown only if there are at least two matches in the list. Setting force_list to an non-empty string forces the list to be shown even if there is only one match.
list_max
Initially this is set to the value of the LISTMAX parameter. It may be set to any other numeric value; when the widget exits this value will be used in the same way as the value of LISTMAX.
last_prompt
If this is set to an non-empty string, the completion code will move the cursor back to the previous prompt after the list of completions has been displayed. Initially this is set or unset according to the ALWAYS_LAST_PROMPT option.
insert
This controls the manner in which a match is inserted into the command line. On entry to the widget function, if it is unset the command line is not to be changed; if set to unambiguous, any prefix common to all matches is to be inserted; if set to menu or automenu the usual behaviour of the MENU_COMPLETE or AUTO_MENU options, respectively, is to be used.

On exit it may be set to any of the values above (where setting it to the empty string is the same as unsetting it), or to a number, in which case the match whose number is given will be inserted into the command line. It may also be set to a string of the form `group:match' which specifies a match from a group of matches to be inserted, counting from 1 upwards (e.g. `2:4' specifies the fourth match of the second group). Negative numbers count backward from the last match or group (with `-1' selecting the last match or group) and out-of-range values are wrapped around, so that a value of zero selects the last match or group and a value one more than the maximum selects the first. Unless the value of this key ends in a space, the match is inserted as in a menu-completion, i.e. without automatically appending a space.

to_end
Specifies the occasions on which the cursor is moved to the end of a string when a match is inserted. On entry to a widget function, it may be single if this will happen when a single unambiguous match was inserted or match if it will happen any time a match is inserted (for example, by menucompletion; this is likely to be the effect of the ALWAYS_TO_END option).

On exit, it may be set to single as above. It may also be set to always, or to the empty string or unset; in those cases the cursor will be moved to the end of the string always or never respectively. Any other string is treated as match.

old_list
This is set to yes if there is still a valid list of completions from a previous completion at the time the widget is invoked. This will usually be the case if and only if the previous editing operation was a completion widget or one of the builtin completion functions. If there is a valid list and it is also currently shown on the screen, the value of this key is shown.

After the widget has exited the value of this key is only used if it was set to keep. In this case the completion code will continue to use this old list. If the widget generated new matches, they will not be used.

old_insert
On entry to the widget this will be set to the number of the match of an old list of completions that is currently inserted into the command line. If no match has been inserted, this is unset.

As with old_list, the value of this key will only be used if it is the string keep. If it was set to this value by the widget and there was an old match inserted into the command line, this match will be kept and if the value of the insert key specifies that another match should be inserted, this will be inserted after the old one.

exact
Controls the behaviour when the REC_EXACT option is set. It will be set to accept if an exact match would be accepted, and will be unset otherwise.
exact_string
The string of an exact match if one was found, otherwise unset.
pattern_match
Locally controls the behaviour given by the GLOB_COMPLETE option. Initially it is set to `*' if and only if the option is set. The completion widget may set it to either of these two values, or to any other non-empty string. If it is non-empty, unquoted metacharacters on the command line will be treated as patterns; if it is `*', then additionally a wildcard `*' is assumed at the cursor position; if it is empty or unset, metacharacters will be treated literally.
pattern_insert
Normally this is set to menu, which specifies that menu-completion will be used whenever the matches were generated using pattern matching. If it is set to any other non-empty string by the user and menu-completion is not selected by other option settings, the code will insert an unambiguous string for the generated matches as with normal completion.
unambiguous
This key is read-only and will always be set to the unambiguous string the completion code has generated for all matches added so far.
unambiguous_cursor
This gives the position the cursor would be placed at if the unambiguous string in the unambiguous key were inserted, relative to the value of that key. The cursor would be placed before the character whose index is given by this key.

Builtin Commands

compgen flags ...

Generate matches according to the given flags. These can be any of the normal option flags (not those for extended completion) supported by the compctl builtin command (see section Programmable Completion Using compctl) except for the -t and -l flags. However, when using the -K flag, the function given as argument to it cannot access the command line with the read builtin command.

The matches will be generated in the same way as if the completion code generated them directly from a compctl-definition with the same flags. The completion code will consider only those matches as possible completions that match the prefix and suffix from the special parameters described above. These strings will be compared with the generated matches using the normal matching rules and any matching specifications given with the -M flag to compgen and the global matching specifications given via the compctl -M spec1 ... builtin command.

The return value is zero if at least one match was added and non-zero otherwise.

compadd [ -qQfnUam ] [ -F array ]
[ -P prefix ] [ -S suffix ]
[ -p hidden-prefix ] [ -s hidden-suffix ]
[ -i ignored-prefix ] [ -I ignored-suffix ]
[ -W file-prefix ] [ -y array ]
[ -J name ] [ -V name ] [ -X explanation ]
[ -r remove-chars ] [ -R remove-func ]
[ -M match-spec ] [ -O array ] [ -A array ]
[ -D array ] [ -- ] [ words ... ]

This builtin command can be used to add matches directly and control all the information the completion code stores with each possible match. The return value is zero if at least one match was added and non-zero if no matches were added.

The completion code breaks the string to complete into seven fields in the order:

<ipre><apre><hpre><word><hsuf><asuf><isuf>

The first field is an ignored prefix taken from the command line, the contents of the IPREFIX parameter plus the string given with the -i option. With the -U option, only the string from the -i option is used. The field <apre> is an optional prefix string given with the -P option. The <hpre> field is a string that is considered part of the match but that should not be shown when listing completions, given with the -p option; for example, functions that do filename generation might specify a common path prefix this way. <word> is the part of the match that should appear in the list of completions, one of the words given at the end. The suffixes <hsuf>, <asuf> and <isuf> correspond to the prefixes <hpre>, <apre> and <ipre> and are given by the options -s, -S and -I, respectively.

The supported flags are:

-P prefix
As for compctl and compgen, it gives a string to be inserted before the given words. The string given is not considered as part of the match.
-S suffix
Like -P but gives a string to be inserted after the match.
-p hidden-prefix
This gives a string that should be inserted into the command line before the match but that should not appear in the list of matches. Unless the -U option is given, this string must be matched as part of the string on the command line.
-s hidden-suffix
Like `-p', but gives a string to insert after the match.
-i ignored-prefix
This gives a string to insert into the command line just before any string given with the `-P' option. Without `-P' the string is inserted before the string given with `-p' or directly before the match.
-I ignored-suffix
Like -i, but gives an ignored suffix.
-y array
This gives a number of strings to display instead of the matches. This is like the -y option of the compctl builtin command but the array argument may only be the name of an array parameter or a literal array in parentheses containing the strings to display.
-J name
As for compctl and compgen, this gives the name of the group of matches the words should be stored in.
-V name
Like -J but naming a unsorted group.
-X explanation
As for compctl and compgen, the explanation string will be printed with the list of matches.
-q
As for compctl and compgen, the suffix given with -S will be automatically removed if the next character typed is a blank or does not insert anything, or if the suffix consists of only one character and the next character typed is the same character.
-r remove-chars
This is a more versatile form of the -q option. The suffix given with -S or the slash automatically added after completing directories will be automatically removed if the next character typed inserts one of the characters given in the remove-chars. This string is parsed as a characters class and understands the backslash sequences used by the print command. For example, `-r "a-z\t"' removes the suffix if the next character typed inserts a lowercase character or a TAB, and `-r "^0-9"' removes the suffix if the next character typed inserts anything but a digit. One extra backslash sequence is understood in this string: `\-' stands for all characters that insert nothing. Thus `-S "=" -q' is the same as `-S "=" -r "= \t\n\-"'.
-R remove-func
This is another form of the -r option. When a suffix has been inserted and the completion accepted, the function remove-func will be called after the next character typed. It is passed the length of the suffix as an argument and can use the special parameters available in ordinary (non-completion) zle widgets (see section Zsh Line Editor) to analyse and modify the command line.
-f
If this flag is given, all of the matches built from words are marked as being the names of files. They are not required to be actual filenames, but if they are, and the option LIST_TYPES is set, the characters describing the types of the files in the completion lists will be shown. This also forces a slash to be added when the name of a directory is completed.
-W file-prefix
This option has the same meaning as for the compctl and compgen builtin commands. Here, however, only one string may be given, not an array. This string is a pathname that will be prepended to each of the matches formed by the given words together with any prefix specified by the -p option to form a complete filename for testing. Hence it is only useful if combined with the -f flag, as the tests will not otherwise be performed.
-a
In the compctl or compgen commands, the completion code normally builds two sets of matches: the normal one where words with one of the suffixes in the array parameter fignore are not considered possible matches, and the alternate set where the words excluded from the first set are stored. Normally only the matches in the first set are used, but if this set is empty, the words from the alternate set are used.

The compadd builtin does not use the fignore parameter and normally stores all words in the first set. With the -a-flag given, however, the given words are stored in the alternate set unless this flag is overridden by the -F option.

-F array
Specifies an array containing suffixes in the same form as the fignore parameter. Words with one of these suffixes are stored in the alternate set of matches and words without one of these suffixes are stored in the normal set.

The array may be the name of an array parameter or a list of literal suffixes enclosed in parentheses and quoted, as in `-F "(.o .h)"'. If the name of an array is given, the elements of the array are taken as the suffixes.

-Q
As for compctl and compgen, this flag instructs the completion code not to quote any metacharacters in the words when inserting them into the command line.
-M match-spec
As for compctl and compgen, this gives local match specifications. Note that they will only be used if the -U option is not given.
-n
Specifies that the words added are to be used as possible matches, but are not to appear in the completion listing.
-U
If this flag is given, all words given will be accepted and no matching will be done by the completion code. Normally this is used in functions that do the matching themselves.

Note that with compadd this option does not automatically turn on menu completion if AUTO_LIST is set, unlike the corresponding option of compctl and compgen commands.

-O array
If this option is given, the words are not added to the set of possible completions. Instead, matching is done as usual and all of the words given as arguments that match the string on the command line will be stored in the array parameter whose name is given as array.
-A array
As the -O option, except that instead of those of the words which match being stored in array, the strings generated internally by the completion code are stored. For example, with a matching specification of `-M "L:|no="', the string `nof' on the command line and the string `foo' as one of the words, this option stores the string `nofoo' in the array, whereas the -O option stores the `foo' originally given.
-D array
As with -O, the words are not added to the set of possible completions. Instead, the completion code tests every word if it matches what is on the line. If the n'th word does not match, the n'th element of the array is removed. Elements for which the corresponding word is matched are retained.
-, --
This flag ends the list of flags and options. All arguments after it will be taken as the words to use as matches even if they begin with hyphens.
compset -p number
compset -P [ number ] pattern
compset -s number
compset -S [ number ] pattern
compset -n begin [ end ]
compset -N beg-pat [ end-pat ]
compset -q
This command simplifies modification of the special parameters, while its return value allows tests on them to be carried out.

The options are:

-p number
If the contents of the PREFIX parameter is longer than number characters, the first number characters are removed from it and appended to the contents of the IPREFIX parameter.
-P [ number ] pattern
If the value of the PREFIX parameter begins with anything that matches the pattern, the matched portion is removed from PREFIX and appended to IPREFIX.

Without the optional number, the longest match is taken, but if number is given, anything up to the number'th match is moved. If the number is negative, the number'th longest match is moved. For example, if PREFIX contains the string `a=b=c', then compset -P '*\=' will move the string `a=b=' into the IPREFIX parameter, but compset -P 1 '*\=' will move only the string `a='.

-s number
As -p, but transfer the last number characters from the value of SUFFIX to the front of the value of ISUFFIX.
-S [ number ] pattern
As -P, but match the last portion of SUFFIX and transfer the matched portion to the front of the value of ISUFFIX.
-n begin [ end ]
If the current word position as specified by the parameter CURRENT is greater than or equal to begin, anything up to the begin'th word is removed from the words array and the value of the parameter CURRENT is decremented by begin.

If the optional end is given, the modification is done only if the current word position is also less than or equal to end. In this case, the words from position end onwards are also removed from the words array.

Both begin and end may be negative to count backwards from the last element of the words array.

-N beg-pat [ end-pat ]
If one of the elements of the words array before the one at the index given by the value of the parameter CURRENT matches the pattern beg-pat, all elements up to and including the matching one are removed from the words array and the value of CURRENT is changed to point to the same word in the changed array.

If the optional pattern end-pat is also given, and there is an element in the words array matching this pattern, the parameters are modified only if the index of this word is higher than the one given by the CURRENT parameter (so that the matching word has to be after the cursor). In this case, the words starting with the one matching end-pat are also removed from the words array. If words contains no word matching end-pat, the testing and modification is performed as if it were not given.

-q
The word currently being completed is split in separate words at the spaces. The resulting words are stored in the words array, and CURRENT, PREFIX, SUFFIX, QIPREFIX, and QISUFFIX are modified to reflect the word part that is completed.

In all the above cases the return value is zero if the test succeeded and the parameters were modified and non-zero otherwise. This allows one to use this builtin in tests such as:

if compset -P '*\='; then ...

This forces anything up to and including the last equal sign to be ignored by the completion code.

compcall [ -TD ]

This allows the use of completions defined with the compctl builtin from within completion widgets. The list of matches will be generated as if one of the non-widget completion function (complete-word, etc.) had been called, except that only compctls given for specific commands are used. To force the code to try completions defined with the -T option of compctl and/or the default completion (whether defined by compctl -D or the builtin default) in the appropriate places, the -T and/or -D flags can be passed to compcall.

The return value can be used to test if a matching compctl definition was found. It is non-zero if a compctl was found and zero otherwise.

Condition Codes

The following additional condition codes for use within the [[ ... ]] construct are available in completion widgets. These work on the special parameters. All of these tests can also be performed by the compset builtin, but in the case of the condition codes the contents of the special parameters are not modified.

-prefix [ number ] pattern
true if the test for the -P option of compset would succeed.
-suffix [ number ] pattern
true if the test for the -S option of compset would succeed.
-after beg-pat
true if the test of the -N option with only the beg-pat given would succeed.
-between beg-pat end-pat
true if the test for the -N option with both patterns would succeed.

Examples

The first step is to define the widget:

zle -C complete complete-word complete-history

Then the widget can be bound to a key using the bindkey builtin command:

bindkey '^X\t' complete

After that the shell function complete-history will be invoked after typing control-X and TAB. The function should then generate the matches, e.g.:

complete-history () { compgen -H 0 '' }

This function will complete words from the history matching the current word.

For a description of the widget-based completion system provided with the source code distribution, see section Completion System.


[Contents]   [Back]   [Prev]   [Up]   [Next]   [Forward]