1ZSHCOMPWID(1) General Commands Manual ZSHCOMPWID(1)
2
3
4
6 zshcompwid - zsh completion widgets
7
9 The shell's programmable completion mechanism can be manipulated in two
10 ways; here the low-level features supporting the newer, function-based
11 mechanism are defined. A complete set of shell functions based on
12 these features is described in zshcompsys(1), and users with no inter‐
13 est in adding to that system (or, potentially, writing their own -- see
14 dictionary entry for `hubris') should skip the current section. The
15 older system based on the compctl builtin command is described in zsh‐
16 compctl(1).
17
18 Completion widgets are defined by the -C option to the zle builtin com‐
19 mand provided by the zsh/zle module (see zshzle(1)). For example,
20
21 zle -C complete expand-or-complete completer
22
23 defines a widget named `complete'. The second argument is the name of
24 any of the builtin widgets that handle completions: complete-word, ex‐
25 pand-or-complete, expand-or-complete-prefix, menu-complete, menu-ex‐
26 pand-or-complete, reverse-menu-complete, list-choices, or
27 delete-char-or-list. Note that this will still work even if the widget
28 in question has been re-bound.
29
30 When this newly defined widget is bound to a key using the bindkey
31 builtin command defined in the zsh/zle module (see zshzle(1)), typing
32 that key will call the shell function `completer'. This function is re‐
33 sponsible for generating completion matches using the builtins de‐
34 scribed below. As with other ZLE widgets, the function is called with
35 its standard input closed.
36
37 Once the function returns, the completion code takes over control again
38 and treats the matches in the same manner as the specified builtin wid‐
39 get, in this case expand-or-complete.
40
42 The parameters ZLE_REMOVE_SUFFIX_CHARS and ZLE_SPACE_SUFFIX_CHARS are
43 used by the completion mechanism, but are not special. See Parameters
44 Used By The Shell in zshparam(1).
45
46 Inside completion widgets, and any functions called from them, some pa‐
47 rameters have special meaning; outside these functions they are not
48 special to the shell in any way. These parameters are used to pass in‐
49 formation between the completion code and the completion widget. Some
50 of the builtin commands and the condition codes use or change the cur‐
51 rent values of these parameters. Any existing values will be hidden
52 during execution of completion widgets; except for compstate, the pa‐
53 rameters are reset on each function exit (including nested function
54 calls from within the completion widget) to the values they had when
55 the function was entered.
56
57 CURRENT
58 This is the number of the current word, i.e. the word the cursor
59 is currently on in the words array. Note that this value is
60 only correct if the ksharrays option is not set.
61
62 IPREFIX
63 Initially this will be set to the empty string. This parameter
64 functions like PREFIX; it contains a string which precedes the
65 one in PREFIX and is not considered part of the list of matches.
66 Typically, a string is transferred from the beginning of PREFIX
67 to the end of IPREFIX, for example:
68
69 IPREFIX=${PREFIX%%\=*}=
70 PREFIX=${PREFIX#*=}
71
72 causes the part of the prefix up to and including the first
73 equal sign not to be treated as part of a matched string. This
74 can be done automatically by the compset builtin, see below.
75
76 ISUFFIX
77 As IPREFIX, but for a suffix that should not be considered part
78 of the matches; note that the ISUFFIX string follows the SUFFIX
79 string.
80
81 PREFIX Initially this will be set to the part of the current word from
82 the beginning of the word up to the position of the cursor; it
83 may be altered to give a common prefix for all matches.
84
85 QIPREFIX
86 This parameter is read-only and contains the quoted string up to
87 the word being completed. E.g. when completing `"foo', this pa‐
88 rameter contains the double quote. If the -q option of compset
89 is used (see below), and the original string was `"foo bar' with
90 the cursor on the `bar', this parameter contains `"foo '.
91
92 QISUFFIX
93 Like QIPREFIX, but containing the suffix.
94
95 SUFFIX Initially this will be set to the part of the current word from
96 the cursor position to the end; it may be altered to give a com‐
97 mon suffix for all matches. It is most useful when the option
98 COMPLETE_IN_WORD is set, as otherwise the whole word on the com‐
99 mand line is treated as a prefix.
100
101 compstate
102 This is an associative array with various keys and values that
103 the completion code uses to exchange information with the com‐
104 pletion widget. The keys are:
105
106 all_quotes
107 The -q option of the compset builtin command (see below)
108 allows a quoted string to be broken into separate words;
109 if the cursor is on one of those words, that word will be
110 completed, possibly invoking `compset -q' recursively.
111 With this key it is possible to test the types of quoted
112 strings which are currently broken into parts in this
113 fashion. Its value contains one character for each quot‐
114 ing level. The characters are a single quote or a double
115 quote for strings quoted with these characters, a dollars
116 sign for strings quoted with $'...' and a backslash for
117 strings not starting with a quote character. The first
118 character in the value always corresponds to the inner‐
119 most quoting level.
120
121 context
122 This will be set by the completion code to the overall
123 context in which completion is attempted. Possible values
124 are:
125
126 array_value
127 when completing inside the value of an array pa‐
128 rameter assignment; in this case the words array
129 contains the words inside the parentheses.
130
131 brace_parameter
132 when completing the name of a parameter in a pa‐
133 rameter expansion beginning with ${. This context
134 will also be set when completing parameter flags
135 following ${(; the full command line argument is
136 presented and the handler must test the value to
137 be completed to ascertain that this is the case.
138
139 assign_parameter
140 when completing the name of a parameter in a pa‐
141 rameter assignment.
142
143 command
144 when completing for a normal command (either in
145 command position or for an argument of the com‐
146 mand).
147
148 condition
149 when completing inside a `[[...]]' conditional ex‐
150 pression; in this case the words array contains
151 only the words inside the conditional expression.
152
153 math when completing in a mathematical environment such
154 as a `((...))' construct.
155
156 parameter
157 when completing the name of a parameter in a pa‐
158 rameter expansion beginning with $ but not ${.
159
160 redirect
161 when completing after a redirection operator.
162
163 subscript
164 when completing inside a parameter subscript.
165
166 value when completing the value of a parameter assign‐
167 ment.
168
169 exact Controls the behaviour when the REC_EXACT option is set.
170 It will be set to accept if an exact match would be ac‐
171 cepted, and will be unset otherwise.
172
173 If it was set when at least one match equal to the string
174 on the line was generated, the match is accepted.
175
176 exact_string
177 The string of an exact match if one was found, otherwise
178 unset.
179
180 ignored
181 The number of completions that were ignored because they
182 matched one of the patterns given with the -F option to
183 the compadd builtin command.
184
185 insert This controls the manner in which a match is inserted
186 into the command line. On entry to the widget function,
187 if it is unset the command line is not to be changed; if
188 set to unambiguous, any prefix common to all matches is
189 to be inserted; if set to automenu-unambiguous, the com‐
190 mon prefix is to be inserted and the next invocation of
191 the completion code may start menu completion (due to the
192 AUTO_MENU option being set); if set to menu or automenu
193 menu completion will be started for the matches currently
194 generated (in the latter case this will happen because
195 the AUTO_MENU is set). The value may also contain the
196 string `tab' when the completion code would normally not
197 really do completion, but only insert the TAB character.
198
199 On exit it may be set to any of the values above (where
200 setting it to the empty string is the same as unsetting
201 it), or to a number, in which case the match whose number
202 is given will be inserted into the command line. Nega‐
203 tive numbers count backward from the last match (with
204 `-1' selecting the last match) and out-of-range values
205 are wrapped around, so that a value of zero selects the
206 last match and a value one more than the maximum selects
207 the first. Unless the value of this key ends in a space,
208 the match is inserted as in a menu completion, i.e. with‐
209 out automatically appending a space.
210
211 Both menu and automenu may also specify the number of the
212 match to insert, given after a colon. For example,
213 `menu:2' says to start menu completion, beginning with
214 the second match.
215
216 Note that a value containing the substring `tab' makes
217 the matches generated be ignored and only the TAB be in‐
218 serted.
219
220 Finally, it may also be set to all, which makes all
221 matches generated be inserted into the line.
222
223 insert_positions
224 When the completion system inserts an unambiguous string
225 into the line, there may be multiple places where charac‐
226 ters are missing or where the character inserted differs
227 from at least one match. The value of this key contains
228 a colon separated list of all these positions, as indexes
229 into the command line.
230
231 last_prompt
232 If this is set to a non-empty string for every match
233 added, the completion code will move the cursor back to
234 the previous prompt after the list of completions has
235 been displayed. Initially this is set or unset according
236 to the ALWAYS_LAST_PROMPT option.
237
238 list This controls whether or how the list of matches will be
239 displayed. If it is unset or empty they will never be
240 listed; if its value begins with list, they will always
241 be listed; if it begins with autolist or ambiguous, they
242 will be listed when the AUTO_LIST or LIST_AMBIGUOUS op‐
243 tions respectively would normally cause them to be.
244
245 If the substring force appears in the value, this makes
246 the list be shown even if there is only one match. Nor‐
247 mally, the list would be shown only if there are at least
248 two matches.
249
250 The value contains the substring packed if the
251 LIST_PACKED option is set. If this substring is given for
252 all matches added to a group, this group will show the
253 LIST_PACKED behavior. The same is done for the
254 LIST_ROWS_FIRST option with the substring rows.
255
256 Finally, if the value contains the string explanations,
257 only the explanation strings, if any, will be listed and
258 if it contains messages, only the messages (added with
259 the -x option of compadd) will be listed. If it contains
260 both explanations and messages both kinds of explanation
261 strings will be listed. It will be set appropriately on
262 entry to a completion widget and may be changed there.
263
264 list_lines
265 This gives the number of lines that are needed to display
266 the full list of completions. Note that to calculate the
267 total number of lines to display you need to add the num‐
268 ber of lines needed for the command line to this value,
269 this is available as the value of the BUFFERLINES special
270 parameter.
271
272 list_max
273 Initially this is set to the value of the LISTMAX parame‐
274 ter. It may be set to any other value; when the widget
275 exits this value will be used in the same way as the
276 value of LISTMAX.
277
278 nmatches
279 The number of matches added by the completion code so
280 far.
281
282 old_insert
283 On entry to the widget this will be set to the number of
284 the match of an old list of completions that is currently
285 inserted into the command line. If no match has been in‐
286 serted, this is unset.
287
288 As with old_list, the value of this key will only be used
289 if it is the string keep. If it was set to this value by
290 the widget and there was an old match inserted into the
291 command line, this match will be kept and if the value of
292 the insert key specifies that another match should be in‐
293 serted, this will be inserted after the old one.
294
295 old_list
296 This is set to yes if there is still a valid list of com‐
297 pletions from a previous completion at the time the wid‐
298 get is invoked. This will usually be the case if and
299 only if the previous editing operation was a completion
300 widget or one of the builtin completion functions. If
301 there is a valid list and it is also currently shown on
302 the screen, the value of this key is shown.
303
304 After the widget has exited the value of this key is only
305 used if it was set to keep. In this case the completion
306 code will continue to use this old list. If the widget
307 generated new matches, they will not be used.
308
309 parameter
310 The name of the parameter when completing in a subscript
311 or in the value of a parameter assignment.
312
313 pattern_insert
314 Normally this is set to menu, which specifies that menu
315 completion will be used whenever a set of matches was
316 generated using pattern_match (see below). If it is set
317 to any other non-empty string by the user and menu com‐
318 pletion is not selected by other option settings, the
319 code will instead insert any common prefix for the gener‐
320 ated matches as with normal completion.
321
322 pattern_match
323 Locally controls the behaviour given by the GLOB_COMPLETE
324 option. Initially it is set to `*' if and only if the
325 option is set. The completion widget may set it to this
326 value, to an empty string (which has the same effect as
327 unsetting it), or to any other non-empty string. If it
328 is non-empty, unquoted metacharacters on the command line
329 will be treated as patterns; if it is `*', then addition‐
330 ally a wildcard `*' is assumed at the cursor position; if
331 it is empty or unset, metacharacters will be treated lit‐
332 erally.
333
334 Note that the match specifications given to the compadd
335 builtin command are not used if this is set to a
336 non-empty string.
337
338 quote When completing inside quotes, this contains the quota‐
339 tion character (i.e. either a single quote, a double
340 quote, or a backtick). Otherwise it is unset.
341
342 quoting
343 When completing inside single quotes, this is set to the
344 string single; inside double quotes, the string double;
345 inside backticks, the string backtick. Otherwise it is
346 unset.
347
348 redirect
349 The redirection operator when completing in a redirection
350 position, i.e. one of <, >, etc.
351
352 restore
353 This is set to auto before a function is entered, which
354 forces the special parameters mentioned above (words,
355 CURRENT, PREFIX, IPREFIX, SUFFIX, and ISUFFIX) to be re‐
356 stored to their previous values when the function exits.
357 If a function unsets it or sets it to any other string,
358 they will not be restored.
359
360 to_end Specifies the occasions on which the cursor is moved to
361 the end of a string when a match is inserted. On entry
362 to a widget function, it may be single if this will hap‐
363 pen when a single unambiguous match was inserted or match
364 if it will happen any time a match is inserted (for exam‐
365 ple, by menu completion; this is likely to be the effect
366 of the ALWAYS_TO_END option).
367
368 On exit, it may be set to single as above. It may also
369 be set to always, or to the empty string or unset; in
370 those cases the cursor will be moved to the end of the
371 string always or never respectively. Any other string is
372 treated as match.
373
374 unambiguous
375 This key is read-only and will always be set to the com‐
376 mon (unambiguous) prefix the completion code has gener‐
377 ated for all matches added so far.
378
379 unambiguous_cursor
380 This gives the position the cursor would be placed at if
381 the common prefix in the unambiguous key were inserted,
382 relative to the value of that key. The cursor would be
383 placed before the character whose index is given by this
384 key.
385
386 unambiguous_positions
387 This contains all positions where characters in the unam‐
388 biguous string are missing or where the character in‐
389 serted differs from at least one of the matches. The po‐
390 sitions are given as indexes into the string given by the
391 value of the unambiguous key.
392
393 vared If completion is called while editing a line using the
394 vared builtin, the value of this key is set to the name
395 of the parameter given as an argument to vared. This key
396 is only set while a vared command is active.
397
398 words This array contains the words present on the command line cur‐
399 rently being edited.
400
402 compadd [ -akqQfenUl12C ] [ -F array ]
403 [-P prefix ] [ -S suffix ]
404 [-p hidden-prefix ] [ -s hidden-suffix ]
405 [-i ignored-prefix ] [ -I ignored-suffix ]
406 [-W file-prefix ] [ -d array ]
407 [-J group-name ] [ -X explanation ] [ -x message ]
408 [-V group-name ] [ -o [ order ] ]
409 [-r remove-chars ] [ -R remove-func ]
410 [-D array ] [ -O array ] [ -A array ]
411 [-E number ]
412 [-M match-spec ] [ -- ] [ completions ... ]
413
414 This builtin command can be used to add matches directly and
415 control all the information the completion code stores with each
416 possible completion. The return status is zero if at least one
417 match was added and non-zero if no matches were added.
418
419 The completion code breaks each match into seven fields in the
420 order:
421
422 <ipre><apre><hpre><body><hsuf><asuf><isuf>
423
424 The first field is an ignored prefix taken from the command
425 line, the contents of the IPREFIX parameter plus the string
426 given with the -i option. With the -U option, only the string
427 from the -i option is used. The field <apre> is an optional pre‐
428 fix string given with the -P option. The <hpre> field is a
429 string that is considered part of the match but that should not
430 be shown when listing completions, given with the -p option; for
431 example, functions that do filename generation might specify a
432 common path prefix this way. <body> is the part of the match
433 that should appear in the list of matches shown to the user.
434 The suffixes <hsuf>, <asuf> and <isuf> correspond to the pre‐
435 fixes <hpre>, <apre> and <ipre> and are given by the options -s,
436 -S and -I, respectively.
437
438 The supported flags are:
439
440 -P prefix
441 This gives a string to be inserted before each match.
442 The string given is not considered as part of the match
443 and any shell metacharacters in it will not be quoted
444 when the string is inserted.
445
446 -S suffix
447 Like -P, but gives a string to be inserted after each
448 match.
449
450 -p hidden-prefix
451 This gives a string that should be inserted before each
452 match but that should not appear in the list of matches.
453 Unless the -U option is given, this string must be
454 matched as part of the string on the command line.
455
456 -s hidden-suffix
457 Like `-p', but gives a string to insert after each match.
458
459 -i ignored-prefix
460 This gives a string to insert just before any string
461 given with the `-P' option. Without `-P' the string is
462 inserted before the string given with `-p' or directly
463 before each match.
464
465 -I ignored-suffix
466 Like -i, but gives an ignored suffix.
467
468 -a With this flag the completions are taken as names of ar‐
469 rays and the actual completions are their values. If
470 only some elements of the arrays are needed, the comple‐
471 tions may also contain subscripts, as in `foo[2,-1]'.
472
473 -k With this flag the completions are taken as names of as‐
474 sociative arrays and the actual completions are their
475 keys. As for -a, the words may also contain subscripts,
476 as in `foo[(R)*bar*]'.
477
478 -d array
479 This adds per-completion display strings. The array
480 should contain one element per completion given. The com‐
481 pletion code will then display the first element instead
482 of the first completion, and so on. The array may be
483 given as the name of an array parameter or directly as a
484 space-separated list of words in parentheses.
485
486 If there are fewer display strings than completions, the
487 leftover completions will be displayed unchanged and if
488 there are more display strings than completions, the
489 leftover display strings will be silently ignored.
490
491 -l This option only has an effect if used together with the
492 -d option. If it is given, the display strings are listed
493 one per line, not arrayed in columns.
494
495 -o [ order ]
496 This controls the order in which matches are sorted. or‐
497 der is a comma-separated list comprising the following
498 possible values. These values can be abbreviated to
499 their initial two or three characters. Note that the or‐
500 der forms part of the group name space so matches with
501 different orderings will not be in the same group.
502
503 match If given, the order of the output is determined by
504 the match strings; otherwise it is determined by
505 the display strings (i.e. the strings given by the
506 -d option). This is the default if `-o' is speci‐
507 fied but the order argument is omitted.
508
509 nosort This specifies that the completions are pre-sorted
510 and their order should be preserved. This value
511 only makes sense alone and cannot be combined with
512 any others.
513
514 numeric
515 If the matches include numbers, sort them numeri‐
516 cally rather than lexicographically.
517
518 reverse
519 Arrange the matches backwards by reversing the
520 sort ordering.
521
522 -J group-name
523 Gives the name of the group that the matches should be
524 stored in.
525
526 -V group-name
527 Like -J but naming an unsorted group. This option is
528 identical to the combination of -J and -o nosort.
529
530 -1 If given together with the -V option, makes only consecu‐
531 tive duplicates in the group be removed. If combined with
532 the -J option, this has no visible effect. Note that
533 groups with and without this flag are in different name
534 spaces.
535
536 -2 If given together with the -J or -V option, makes all du‐
537 plicates be kept. Again, groups with and without this
538 flag are in different name spaces.
539
540 -X explanation
541 The explanation string will be printed with the list of
542 matches, above the group currently selected.
543
544 Within the explanation, the following sequences may be
545 used to specify output attributes as described in the
546 section EXPANSION OF PROMPT SEQUENCES in zshmisc(1):
547 `%B', `%S', `%U', `%F', `%K' and their lower case coun‐
548 terparts, as well as `%{...%}'. `%F', `%K' and `%{...%}'
549 take arguments in the same form as prompt expansion.
550 (Note that the sequence `%G' is not available; an argu‐
551 ment to `%{' should be used instead.) The sequence `%%'
552 produces a literal `%'.
553
554 These sequences are most often employed by users when
555 customising the format style (see zshcompsys(1)), but
556 they must also be taken into account when writing comple‐
557 tion functions, as passing descriptions with unescaped
558 `%' characters to utility functions such as _arguments
559 and _message may produce unexpected results. If arbitrary
560 text is to be passed in a description, it can be escaped
561 using e.g. ${my_str//\%/%%}.
562
563 -x message
564 Like -X, but the message will be printed even if there
565 are no matches in the group.
566
567 -q The suffix given with -S will be automatically removed if
568 the next character typed is a blank or does not insert
569 anything, or if the suffix consists of only one character
570 and the next character typed is the same character.
571
572 -r remove-chars
573 This is a more versatile form of the -q option. The suf‐
574 fix given with -S or the slash automatically added after
575 completing directories will be automatically removed if
576 the next character typed inserts one of the characters
577 given in the remove-chars. This string is parsed as a
578 characters class and understands the backslash sequences
579 used by the print command. For example, `-r "a-z\t"' re‐
580 moves the suffix if the next character typed inserts a
581 lower case character or a TAB, and `-r "^0-9"' removes
582 the suffix if the next character typed inserts anything
583 but a digit. One extra backslash sequence is understood
584 in this string: `\-' stands for all characters that in‐
585 sert nothing. Thus `-S "=" -q' is the same as `-S "=" -r
586 "= \t\n\-"'.
587
588 This option may also be used without the -S option; then
589 any automatically added space will be removed when one of
590 the characters in the list is typed.
591
592 -R remove-func
593 This is another form of the -r option. When a match has
594 been accepted and a suffix has been inserted, the func‐
595 tion remove-func will be called after the next character
596 typed. It is passed the length of the suffix as an argu‐
597 ment and can use the special parameters available in or‐
598 dinary (non-completion) zle widgets (see zshzle(1)) to
599 analyse and modify the command line.
600
601 -f If this flag is given, all of the matches built from the
602 completions are marked as being the names of files. They
603 are not required to be actual filenames, but if they are,
604 and the option LIST_TYPES is set, the characters describ‐
605 ing the types of the files in the completion lists will
606 be shown. This also forces a slash to be added when the
607 name of a directory is completed.
608
609 -e This flag can be used to tell the completion code that
610 the matches added are parameter names for a parameter ex‐
611 pansion. This will make the AUTO_PARAM_SLASH and
612 AUTO_PARAM_KEYS options be used for the matches.
613
614 -W file-prefix
615 This string is a pathname that will be prepended to each
616 match together with any prefix specified by the -p option
617 to form a complete filename for testing. Hence it is
618 only useful if combined with the -f flag, as the tests
619 will not otherwise be performed.
620
621 -F array
622 Specifies an array containing patterns. completions that
623 match one of these patterns are ignored, that is, not
624 considered to be matches.
625
626 The array may be the name of an array parameter or a list
627 of literal patterns enclosed in parentheses and quoted,
628 as in `-F "(*?.o *?.h)"'. If the name of an array is
629 given, the elements of the array are taken as the pat‐
630 terns.
631
632 -Q This flag instructs the completion code not to quote any
633 metacharacters in the matches when inserting them into
634 the command line.
635
636 -M match-spec
637 This gives local match specifications as described below
638 in the section `Completion Matching Control'. This option
639 may be given more than once. In this case all
640 match-specs given are concatenated with spaces between
641 them to form the specification string to use. Note that
642 they will only be used if the -U option is not given.
643
644 -n Specifies that matching completions are to be added to
645 the set of matches, but are not to be listed to the user.
646
647 -U If this flag is given, all completions are added to the
648 set of matches and no matching will be done by the com‐
649 pletion code. Normally this is used in functions that do
650 the matching themselves.
651
652 -O array
653 If this option is given, the completions are not added to
654 the set of matches. Instead, matching is done as usual
655 and all of the completions that match will be stored in
656 the array parameter whose name is given as array.
657
658 -A array
659 As the -O option, except that instead of those of the
660 completions which match being stored in array, the
661 strings generated internally by the completion code are
662 stored. For example, with a match specification of `-M
663 "L:|no="', a current word of `nof' and completions of
664 `foo', this option stores the string `nofoo' in the ar‐
665 ray, whereas the -O option stores the `foo' originally
666 given.
667
668 -D array
669 As with -O, the completions are not added to the set of
670 matches. Instead, whenever the nth completion does not
671 match, the nth element of the array is removed. Elements
672 for which the corresponding completion matches are re‐
673 tained. This option can be used more than once to remove
674 elements from multiple arrays.
675
676 -C This option adds a special match which expands to all
677 other matches when inserted into the line, even those
678 that are added after this option is used. Together with
679 the -d option it is possible to specify a string that
680 should be displayed in the list for this special match.
681 If no string is given, it will be shown as a string con‐
682 taining the strings that would be inserted for the other
683 matches, truncated to the width of the screen.
684
685 -E number
686 This option adds number empty matches after matching com‐
687 pletions have been added. An empty match takes up space
688 in completion listings but will never be inserted in the
689 line and can't be selected with menu completion or menu
690 selection. This makes empty matches only useful to for‐
691 mat completion lists and to make explanatory string be
692 shown in completion lists (since empty matches can be
693 given display strings with the -d option). And because
694 all but one empty string would otherwise be removed, this
695 option implies the -V and -2 options (even if an explicit
696 -J option is given). This can be important to note as it
697 affects the name space into which matches are added.
698
699 -
700 -- This flag ends the list of flags and options. All argu‐
701 ments after it will be taken as the completions even if
702 they begin with hyphens.
703
704 Except for the -M flag, if any of these flags is given more than
705 once, the first one (and its argument) will be used.
706
707 compset -p number
708 compset -P [ number ] pattern
709 compset -s number
710 compset -S [ number ] pattern
711 compset -n begin [ end ]
712 compset -N beg-pat [ end-pat ]
713 compset -q
714 This command simplifies modification of the special parameters,
715 while its return status allows tests on them to be carried out.
716
717 The options are:
718
719 -p number
720 If the value of the PREFIX parameter is at least number
721 characters long, the first number characters are removed
722 from it and appended to the contents of the IPREFIX pa‐
723 rameter.
724
725 -P [ number ] pattern
726 If the value of the PREFIX parameter begins with anything
727 that matches the pattern, the matched portion is removed
728 from PREFIX and appended to IPREFIX.
729
730 Without the optional number, the longest match is taken,
731 but if number is given, anything up to the numberth match
732 is moved. If the number is negative, the numberth long‐
733 est match is moved. For example, if PREFIX contains the
734 string `a=b=c', then compset -P '*\=' will move the
735 string `a=b=' into the IPREFIX parameter, but compset -P
736 1 '*\=' will move only the string `a='.
737
738 -s number
739 As -p, but transfer the last number characters from the
740 value of SUFFIX to the front of the value of ISUFFIX.
741
742 -S [ number ] pattern
743 As -P, but match the last portion of SUFFIX and transfer
744 the matched portion to the front of the value of ISUFFIX.
745
746 -n begin [ end ]
747 If the current word position as specified by the parame‐
748 ter CURRENT is greater than or equal to begin, anything
749 up to the beginth word is removed from the words array
750 and the value of the parameter CURRENT is decremented by
751 begin.
752
753 If the optional end is given, the modification is done
754 only if the current word position is also less than or
755 equal to end. In this case, the words from position end
756 onwards are also removed from the words array.
757
758 Both begin and end may be negative to count backwards
759 from the last element of the words array.
760
761 -N beg-pat [ end-pat ]
762 If one of the elements of the words array before the one
763 at the index given by the value of the parameter CURRENT
764 matches the pattern beg-pat, all elements up to and in‐
765 cluding the matching one are removed from the words array
766 and the value of CURRENT is changed to point to the same
767 word in the changed array.
768
769 If the optional pattern end-pat is also given, and there
770 is an element in the words array matching this pattern,
771 the parameters are modified only if the index of this
772 word is higher than the one given by the CURRENT parame‐
773 ter (so that the matching word has to be after the cur‐
774 sor). In this case, the words starting with the one
775 matching end-pat are also removed from the words array.
776 If words contains no word matching end-pat, the testing
777 and modification is performed as if it were not given.
778
779 -q The word currently being completed is split on spaces
780 into separate words, respecting the usual shell quoting
781 conventions. The resulting words are stored in the words
782 array, and CURRENT, PREFIX, SUFFIX, QIPREFIX, and QISUF‐
783 FIX are modified to reflect the word part that is com‐
784 pleted.
785
786 In all the above cases the return status is zero if the test
787 succeeded and the parameters were modified and non-zero other‐
788 wise. This allows one to use this builtin in tests such as:
789
790 if compset -P '*\='; then ...
791
792 This forces anything up to and including the last equal sign to
793 be ignored by the completion code.
794
795 compcall [ -TD ]
796 This allows the use of completions defined with the compctl
797 builtin from within completion widgets. The list of matches
798 will be generated as if one of the non-widget completion func‐
799 tions (complete-word, etc.) had been called, except that only
800 compctls given for specific commands are used. To force the code
801 to try completions defined with the -T option of compctl and/or
802 the default completion (whether defined by compctl -D or the
803 builtin default) in the appropriate places, the -T and/or -D
804 flags can be passed to compcall.
805
806 The return status can be used to test if a matching compctl def‐
807 inition was found. It is non-zero if a compctl was found and
808 zero otherwise.
809
810 Note that this builtin is defined by the zsh/compctl module.
811
813 The following additional condition codes for use within the [[ ... ]]
814 construct are available in completion widgets. These work on the spe‐
815 cial parameters. All of these tests can also be performed by the
816 compset builtin, but in the case of the condition codes the contents of
817 the special parameters are not modified.
818
819 -prefix [ number ] pattern
820 true if the test for the -P option of compset would succeed.
821
822 -suffix [ number ] pattern
823 true if the test for the -S option of compset would succeed.
824
825 -after beg-pat
826 true if the test of the -N option with only the beg-pat given
827 would succeed.
828
829 -between beg-pat end-pat
830 true if the test for the -N option with both patterns would suc‐
831 ceed.
832
834 When the user invokes completion, the current word on the command line
835 (that is, the word the cursor is currently on) is used to generate a
836 match pattern. Only those completions that match the pattern are of‐
837 fered to the user as matches.
838
839 The default match pattern is generated from the current word by either
840
841 • appending a `*' (matching any number of characters in a comple‐
842 tion) or,
843
844 • if the shell option COMPLETE_IN_WORD is set, inserting a `*' at
845 the cursor position.
846
847 This narrow pattern can be broadened selectively by passing a match
848 specification to the compadd builtin command through its -M option (see
849 `Completion Builtin Commands' above). A match specification consists
850 of one or more matchers separated by whitespace. Matchers in a match
851 specification are applied one at a time, from left to right. Once all
852 matchers have been applied, completions are compared to the final match
853 pattern and non-matching ones are discarded.
854
855 • Note that the -M option is ignored if the current word contains
856 a glob pattern and the shell option GLOB_COMPLETE is set or if
857 the pattern_match key of the special associative array compstate
858 is set to a non-empty value (see `Completion Special Parameters'
859 above).
860
861 • Users of the completion system (see zshcompsys(1)) should gen‐
862 erally not use the -M option directly, but rather use the
863 matcher-list and matcher styles (see the subsection Standard
864 Styles in the documentation for COMPLETION SYSTEM CONFIGURATION
865 in zshcompsys(1)).
866
867 Each matcher consists of
868
869 • a case-sensitive letter
870
871 • a `:',
872
873 • one or more patterns separated by pipes (`|'),
874
875 • an equals sign (`='), and
876
877 • another pattern.
878
879 The patterns before the `=' are used to match substrings of the current
880 word. For each matched substring, the corresponding part of the match
881 pattern is broadened with the pattern after the `=', by means of a log‐
882 ical OR.
883
884 Each pattern in a matcher cosists of either
885
886 • the empty string or
887
888 • a sequence of
889
890 • literal characters (which may be quoted with a `\'),
891
892 • question marks (`?'),
893
894 • bracket expressions (`[...]'; see the subsection Glob Op‐
895 erators in the documentation for GLOB OPERATORS in zsh‐
896 expn(1)), and/or
897
898 • brace expressions (see below).
899
900
901 Other shell patterns are not allowed.
902
903 A brace expression, like a bracket expression, consists of a list of
904
905 • literal characters,
906
907 • ranges (`0-9'), and/or
908
909 • character classes (`[:name:]').
910
911 However, they differ from each other as follows:
912
913 • A brace expression is delimited by a pair of braces (`{...}').
914
915 • Brace expressions do not support negations. That is, an initial
916 `!' or `^' has no special meaning and will be interpreted as a
917 literal character.
918
919 • When a character in the current word matches the nth pattern in
920 a brace expression, the corresponding part of the match pattern
921 is broadened only with the nth pattern of the brace expression
922 on the other side of the `=', if there is one; if there is no
923 brace expression on the other side, then this pattern is the
924 empty string. However, if either brace expression has more ele‐
925 ments than the other, then the excess entries are simply ig‐
926 nored. When comparing indexes, each literal character or char‐
927 acter class counts as one element, but each range is instead ex‐
928 panded to the full list of literal characters it represents.
929 Additionally, if on both sides of the `=', the nth pattern is
930 `[:upper:]' or `[:lower:]', then these are expanded as ranges,
931 too.
932
933 Note that, although the matching system does not yet handle multibyte
934 characters, this is likely to be a future extension. Hence, using
935 `[:upper:]' and `[:lower:]' is recommended over `A-Z' and `a-z'.
936
937 Below are the different forms of matchers supported. Each uppercase
938 form behaves exactly like its lowercase counterpart, but adds an addi‐
939 tional step after the match pattern has filtered out non-matching com‐
940 pletions: Each of a match's substrings that was matched by a subpat‐
941 tern from an uppercase matcher is replaced with the corresponding sub‐
942 string of the current word. However, patterns from lowercase matchers
943 have higher weight: If a substring of the current word was matched by
944 patterns from both a lowercase and an uppercase matcher, then the low‐
945 ercase matcher's pattern wins and the corresponding part of the match
946 is not modified.
947
948 Unless indicated otherwise, each example listed assumes COM‐
949 PLETE_IN_WORD to be unset (as it is by default).
950
951 m:word-pat=match-pat
952 M:word-pat=match-pat
953
954 For each substring of the current word that matches word-pat,
955 broaden the corresponding part of the match pattern to addition‐
956 ally match match-pat.
957
958 Examples:
959
960 m:{[:lower:]}={[:upper:]} lets any lower case character
961 in the current word be completed to itself or its upper‐
962 case counterpart. So, the completions `foo', `FOO' and
963 `Foo' will are be considered matches for the word `fo'.
964
965 M:_= inserts every underscore from the current word into
966 each match, in the same relative position, determined by
967 matching the substrings around it. So, given a comple‐
968 tion `foo', the word `f_o' will be completed to the match
969 `f_oo', even though the latter was not present as a com‐
970 pletion.
971
972 b:word-pat=match-pat
973 B:word-pat=match-pat
974 e:word-pat=match-pat
975 E:word-pat=match-pat
976
977 For each consecutive substring at the b:eginning or e:nd of the
978 current word that matches word-pat, broaden the corresponding
979 part of the match pattern to additionally match match-pat.
980
981 Examples:
982
983 `b:-=+' lets any number of minuses at the start of the
984 current word be completed to a minus or a plus.
985
986 `B:0=' adds all zeroes at the beginning of the current
987 word to the beginning of each match.
988
989 l:|word-pat=match-pat
990 L:|word-pat=match-pat
991 R:word-pat|=match-pat
992 r:word-pat|=match-pat
993
994 If there is a substring at the l:eft or r:ight edge of the cur‐
995 rent word that matches word-pat, then broaden the corresponding
996 part of the match pattern to additionally match match-pat.
997
998 For each l:, L:, r: and R: matcher (including the ones below),
999 the pattern match-pat may also be a `*'. This matches any num‐
1000 ber of characters in a completion.
1001
1002 Examples:
1003
1004 `r:|=*' appends a `*' to the match pattern, even when
1005 COMPLETE_IN_WORD is set and the cursor is not at the end
1006 of the current word.
1007
1008 If the current word starts with a minus, then `L:|-='
1009 will prepend it to each match.
1010
1011 l:anchor|word-pat=match-pat
1012 L:anchor|word-pat=match-pat
1013 r:word-pat|anchor=match-pat
1014 R:word-pat|anchor=match-pat
1015
1016 For each substring of the current word that matches word-pat and
1017 has on its l:eft or r:ight another substring matching anchor,
1018 broaden the corresponding part of the match pattern to addition‐
1019 ally match match-pat.
1020
1021 Note that these matchers (and the ones below) modify only what
1022 is matched by word-pat; they do not change the matching behavior
1023 of what is matched by anchor (or coanchor; see the matchers be‐
1024 low). Thus, unless its corresponding part of the match pattern
1025 has been modified, the anchor in the current word has to match
1026 literally in each completion, just like any other substring of
1027 the current word.
1028
1029 If a matcher includes at least one anchor (which includes the
1030 matchers with two anchors, below), then match-pat may also be
1031 `*' or `**'. `*' can match any part of a completion that does
1032 not contain any substrings matching anchor, whereas a `**' can
1033 match any part of a completion, period. (Note that this is dif‐
1034 ferent from the behavior of `*' in the anchorless forms of `l:'
1035 and `r:' and and also different from `*' and `**' in glob ex‐
1036 pressions.)
1037
1038 Examples:
1039
1040 `r:|.=*' makes the completion `comp.sources.unix' a match
1041 for the word `..u' -- but not for the word `.u'.
1042
1043 Given a completion `--foo', the matcher `L:--|no-=' will
1044 complete the word `--no-' to the match `--no-foo'.
1045
1046 l:anchor||coanchor=match-pat
1047 L:anchor||coanchor=match-pat
1048 r:coanchor||anchor=match-pat
1049 R:coanchor||anchor=match-pat
1050
1051 For any two consecutive substrings of the current word that
1052 match anchor and coanchor, in the order given, insert the pat‐
1053 tern match-pat between their corresponding parts in the match
1054 pattern.
1055
1056 Note that, unlike anchor, the pattern coanchor does not change
1057 what `*' can match.
1058
1059 Examples:
1060
1061 `r:?||[[:upper:]]=*' will complete the current word `fB'
1062 to `fooBar', but it will not complete it to `fooHooBar'
1063 (because `*' here cannot match anything that includes a
1064 match for `[[:upper:]]), nor will it complete `B' to
1065 `fooBar' (because there is no character in the current
1066 word to match coanchor).
1067
1068 Given the current word `pass.n' and a completion
1069 `pass.byname', the matcher `L:.||[[:alpha:]]=by' will
1070 produce the match `pass.name'.
1071
1072 x:
1073
1074 Ignore this matcher and all matchers to its right.
1075
1076 This matcher is used to mark the end of a match specification.
1077 In a single standalone list of matchers, this has no use, but
1078 where match specifications are concatenated, as is often the
1079 case when using the completion system (see zshcompsys(1)), it
1080 can allow one match specification to override another.
1081
1083 The first step is to define the widget:
1084
1085 zle -C complete complete-word complete-files
1086
1087 Then the widget can be bound to a key using the bindkey builtin com‐
1088 mand:
1089
1090 bindkey '^X\t' complete
1091
1092 After that the shell function complete-files will be invoked after typ‐
1093 ing control-X and TAB. The function should then generate the matches,
1094 e.g.:
1095
1096 complete-files () { compadd - * }
1097
1098 This function will complete files in the current directory matching the
1099 current word.
1100
1101
1102
1103zsh 5.9 May 14, 2022 ZSHCOMPWID(1)