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 the possible 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 words 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 generated and accepted by the com‐
280 pletion code so 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 matching. If it is set to any
317 other non-empty string by the user and menu completion is
318 not selected by other option settings, the code will in‐
319 stead insert any common prefix for the generated matches
320 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 matcher 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 ] [ -- ] [ words ... ]
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 match. The return status is zero if at least one match
417 was added and non-zero if no matches were added.
418
419 The completion code breaks the string to complete into seven
420 fields in the order:
421
422 <ipre><apre><hpre><word><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. <word> is the part of the match
433 that should appear in the list of completions, i.e. one of the
434 words given at the end of the compadd command line. The suffixes
435 <hsuf>, <asuf> and <isuf> correspond to the prefixes <hpre>,
436 <apre> and <ipre> and are given by the options -s, -S and -I,
437 respectively.
438
439 The supported flags are:
440
441 -P prefix
442 This gives a string to be inserted before the given
443 words. The string given is not considered as part of the
444 match and any shell metacharacters in it will not be
445 quoted when the string is inserted.
446
447 -S suffix
448 Like -P, but gives a string to be inserted after the
449 match.
450
451 -p hidden-prefix
452 This gives a string that should be inserted into the com‐
453 mand line before the match but that should not appear in
454 the list of matches. Unless the -U option is given, this
455 string must be matched as part of the string on the com‐
456 mand line.
457
458 -s hidden-suffix
459 Like `-p', but gives a string to insert after the match.
460
461 -i ignored-prefix
462 This gives a string to insert into the command line just
463 before any string given with the `-P' option. Without
464 `-P' the string is inserted before the string given with
465 `-p' or directly before the match.
466
467 -I ignored-suffix
468 Like -i, but gives an ignored suffix.
469
470 -a With this flag the words are taken as names of arrays and
471 the possible matches are their values. If only some ele‐
472 ments of the arrays are needed, the words may also con‐
473 tain subscripts, as in `foo[2,-1]'.
474
475 -k With this flag the words are taken as names of associa‐
476 tive arrays and the possible matches are their keys. As
477 for -a, the words may also contain subscripts, as in
478 `foo[(R)*bar*]'.
479
480 -d array
481 This adds per-match display strings. The array should
482 contain one element per word given. The completion code
483 will then display the first element instead of the first
484 word, and so on. The array may be given as the name of an
485 array parameter or directly as a space-separated list of
486 words in parentheses.
487
488 If there are fewer display strings than words, the left‐
489 over words will be displayed unchanged and if there are
490 more display strings than words, the leftover display
491 strings will be silently ignored.
492
493 -l This option only has an effect if used together with the
494 -d option. If it is given, the display strings are listed
495 one per line, not arrayed in columns.
496
497 -o [ order ]
498 This controls the order in which matches are sorted. or‐
499 der is a comma-separated list comprising the following
500 possible values. These values can be abbreviated to
501 their initial two or three characters. Note that the or‐
502 der forms part of the group name space so matches with
503 different orderings will not be in the same group.
504
505 match If given, the order of the output is determined by
506 the match strings; otherwise it is determined by
507 the display strings (i.e. the strings given by the
508 -d option). This is the default if `-o' is speci‐
509 fied but the order argument is omitted.
510
511 nosort This specifies that the matches are pre-sorted and
512 their order should be preserved. This value only
513 makes sense alone and cannot be combined with any
514 others.
515
516 numeric
517 If the matches include numbers, sort them numeri‐
518 cally rather than lexicographically.
519
520 reverse
521 Arrange the matches backwards by reversing the
522 sort ordering.
523
524 -J group-name
525 Gives the name of the group of matches the words should
526 be stored in.
527
528 -V group-name
529 Like -J but naming an unsorted group. This option is
530 identical to the combination of -J and -o nosort.
531
532 -1 If given together with the -V option, makes only consecu‐
533 tive duplicates in the group be removed. If combined with
534 the -J option, this has no visible effect. Note that
535 groups with and without this flag are in different name
536 spaces.
537
538 -2 If given together with the -J or -V option, makes all du‐
539 plicates be kept. Again, groups with and without this
540 flag are in different name spaces.
541
542 -X explanation
543 The explanation string will be printed with the list of
544 matches, above the group currently selected.
545
546 Within the explanation, the following sequences may be
547 used to specify output attributes as described in the
548 section EXPANSION OF PROMPT SEQUENCES in zshmisc(1):
549 `%B', `%S', `%U', `%F', `%K' and their lower case coun‐
550 terparts, as well as `%{...%}'. `%F', `%K' and `%{...%}'
551 take arguments in the same form as prompt expansion.
552 (Note that the sequence `%G' is not available; an argu‐
553 ment to `%{' should be used instead.) The sequence `%%'
554 produces a literal `%'.
555
556 These sequences are most often employed by users when
557 customising the format style (see zshcompsys(1)), but
558 they must also be taken into account when writing comple‐
559 tion functions, as passing descriptions with unescaped
560 `%' characters to utility functions such as _arguments
561 and _message may produce unexpected results. If arbitrary
562 text is to be passed in a description, it can be escaped
563 using e.g. ${my_str//\%/%%}.
564
565 -x message
566 Like -X, but the message will be printed even if there
567 are no matches in the group.
568
569 -q The suffix given with -S will be automatically removed if
570 the next character typed is a blank or does not insert
571 anything, or if the suffix consists of only one character
572 and the next character typed is the same character.
573
574 -r remove-chars
575 This is a more versatile form of the -q option. The suf‐
576 fix given with -S or the slash automatically added after
577 completing directories will be automatically removed if
578 the next character typed inserts one of the characters
579 given in the remove-chars. This string is parsed as a
580 characters class and understands the backslash sequences
581 used by the print command. For example, `-r "a-z\t"' re‐
582 moves the suffix if the next character typed inserts a
583 lower case character or a TAB, and `-r "^0-9"' removes
584 the suffix if the next character typed inserts anything
585 but a digit. One extra backslash sequence is understood
586 in this string: `\-' stands for all characters that in‐
587 sert nothing. Thus `-S "=" -q' is the same as `-S "=" -r
588 "= \t\n\-"'.
589
590 This option may also be used without the -S option; then
591 any automatically added space will be removed when one of
592 the characters in the list is typed.
593
594 -R remove-func
595 This is another form of the -r option. When a suffix has
596 been inserted and the completion accepted, the function
597 remove-func will be called after the next character
598 typed. It is passed the length of the suffix as an argu‐
599 ment and can use the special parameters available in or‐
600 dinary (non-completion) zle widgets (see zshzle(1)) to
601 analyse and modify the command line.
602
603 -f If this flag is given, all of the matches built from
604 words are marked as being the names of files. They are
605 not required to be actual filenames, but if they are, and
606 the option LIST_TYPES is set, the characters describing
607 the types of the files in the completion lists will be
608 shown. This also forces a slash to be added when the name
609 of a directory is completed.
610
611 -e This flag can be used to tell the completion code that
612 the matches added are parameter names for a parameter ex‐
613 pansion. This will make the AUTO_PARAM_SLASH and
614 AUTO_PARAM_KEYS options be used for the matches.
615
616 -W file-prefix
617 This string is a pathname that will be prepended to each
618 of the matches formed by the given words together with
619 any prefix specified by the -p option to form a complete
620 filename for testing. Hence it is only useful if com‐
621 bined with the -f flag, as the tests will not otherwise
622 be performed.
623
624 -F array
625 Specifies an array containing patterns. Words matching
626 one of these patterns are ignored, i.e. not considered to
627 be possible matches.
628
629 The array may be the name of an array parameter or a list
630 of literal patterns enclosed in parentheses and quoted,
631 as in `-F "(*?.o *?.h)"'. If the name of an array is
632 given, the elements of the array are taken as the pat‐
633 terns.
634
635 -Q This flag instructs the completion code not to quote any
636 metacharacters in the words when inserting them into the
637 command line.
638
639 -M match-spec
640 This gives local match specifications as described below
641 in the section `Completion Matching Control'. This option
642 may be given more than once. In this case all
643 match-specs given are concatenated with spaces between
644 them to form the specification string to use. Note that
645 they will only be used if the -U option is not given.
646
647 -n Specifies that the words added are to be used as possible
648 matches, but are not to appear in the completion listing.
649
650 -U If this flag is given, all words given will be accepted
651 and no matching will be done by the completion code. Nor‐
652 mally this is used in functions that do the matching
653 themselves.
654
655 -O array
656 If this option is given, the words are not added to the
657 set of possible completions. Instead, matching is done
658 as usual and all of the words given as arguments that
659 match the string on the command line will be stored in
660 the array parameter whose name is given as array.
661
662 -A array
663 As the -O option, except that instead of those of the
664 words which match being stored in array, the strings gen‐
665 erated internally by the completion code are stored. For
666 example, with a matching specification of `-M "L:|no="',
667 the string `nof' on the command line and the string `foo'
668 as one of the words, this option stores the string `no‐
669 foo' in the array, whereas the -O option stores the `foo'
670 originally given.
671
672 -D array
673 As with -O, the words are not added to the set of possi‐
674 ble completions. Instead, the completion code tests
675 whether each word in turn matches what is on the line.
676 If the nth word does not match, the nth element of the
677 array is removed. Elements for which the corresponding
678 word is matched are retained.
679
680 -C This option adds a special match which expands to all
681 other matches when inserted into the line, even those
682 that are added after this option is used. Together with
683 the -d option it is possible to specify a string that
684 should be displayed in the list for this special match.
685 If no string is given, it will be shown as a string con‐
686 taining the strings that would be inserted for the other
687 matches, truncated to the width of the screen.
688
689 -E number
690 This option adds number empty matches after the words
691 have been added. An empty match takes up space in com‐
692 pletion listings but will never be inserted in the line
693 and can't be selected with menu completion or menu selec‐
694 tion. This makes empty matches only useful to format
695 completion lists and to make explanatory string be shown
696 in completion lists (since empty matches can be given
697 display strings with the -d option). And because all but
698 one empty string would otherwise be removed, this option
699 implies the -V and -2 options (even if an explicit -J op‐
700 tion is given). This can be important to note as it af‐
701 fects the name space into which matches are added.
702
703 -
704 -- This flag ends the list of flags and options. All argu‐
705 ments after it will be taken as the words to use as
706 matches even if they begin with hyphens.
707
708 Except for the -M flag, if any of these flags is given more than
709 once, the first one (and its argument) will be used.
710
711 compset -p number
712 compset -P [ number ] pattern
713 compset -s number
714 compset -S [ number ] pattern
715 compset -n begin [ end ]
716 compset -N beg-pat [ end-pat ]
717 compset -q
718 This command simplifies modification of the special parameters,
719 while its return status allows tests on them to be carried out.
720
721 The options are:
722
723 -p number
724 If the value of the PREFIX parameter is at least number
725 characters long, the first number characters are removed
726 from it and appended to the contents of the IPREFIX pa‐
727 rameter.
728
729 -P [ number ] pattern
730 If the value of the PREFIX parameter begins with anything
731 that matches the pattern, the matched portion is removed
732 from PREFIX and appended to IPREFIX.
733
734 Without the optional number, the longest match is taken,
735 but if number is given, anything up to the numberth match
736 is moved. If the number is negative, the numberth long‐
737 est match is moved. For example, if PREFIX contains the
738 string `a=b=c', then compset -P '*\=' will move the
739 string `a=b=' into the IPREFIX parameter, but compset -P
740 1 '*\=' will move only the string `a='.
741
742 -s number
743 As -p, but transfer the last number characters from the
744 value of SUFFIX to the front of the value of ISUFFIX.
745
746 -S [ number ] pattern
747 As -P, but match the last portion of SUFFIX and transfer
748 the matched portion to the front of the value of ISUFFIX.
749
750 -n begin [ end ]
751 If the current word position as specified by the parame‐
752 ter CURRENT is greater than or equal to begin, anything
753 up to the beginth word is removed from the words array
754 and the value of the parameter CURRENT is decremented by
755 begin.
756
757 If the optional end is given, the modification is done
758 only if the current word position is also less than or
759 equal to end. In this case, the words from position end
760 onwards are also removed from the words array.
761
762 Both begin and end may be negative to count backwards
763 from the last element of the words array.
764
765 -N beg-pat [ end-pat ]
766 If one of the elements of the words array before the one
767 at the index given by the value of the parameter CURRENT
768 matches the pattern beg-pat, all elements up to and in‐
769 cluding the matching one are removed from the words array
770 and the value of CURRENT is changed to point to the same
771 word in the changed array.
772
773 If the optional pattern end-pat is also given, and there
774 is an element in the words array matching this pattern,
775 the parameters are modified only if the index of this
776 word is higher than the one given by the CURRENT parame‐
777 ter (so that the matching word has to be after the cur‐
778 sor). In this case, the words starting with the one
779 matching end-pat are also removed from the words array.
780 If words contains no word matching end-pat, the testing
781 and modification is performed as if it were not given.
782
783 -q The word currently being completed is split on spaces
784 into separate words, respecting the usual shell quoting
785 conventions. The resulting words are stored in the words
786 array, and CURRENT, PREFIX, SUFFIX, QIPREFIX, and QISUF‐
787 FIX are modified to reflect the word part that is com‐
788 pleted.
789
790 In all the above cases the return status is zero if the test
791 succeeded and the parameters were modified and non-zero other‐
792 wise. This allows one to use this builtin in tests such as:
793
794 if compset -P '*\='; then ...
795
796 This forces anything up to and including the last equal sign to
797 be ignored by the completion code.
798
799 compcall [ -TD ]
800 This allows the use of completions defined with the compctl
801 builtin from within completion widgets. The list of matches
802 will be generated as if one of the non-widget completion func‐
803 tions (complete-word, etc.) had been called, except that only
804 compctls given for specific commands are used. To force the code
805 to try completions defined with the -T option of compctl and/or
806 the default completion (whether defined by compctl -D or the
807 builtin default) in the appropriate places, the -T and/or -D
808 flags can be passed to compcall.
809
810 The return status can be used to test if a matching compctl def‐
811 inition was found. It is non-zero if a compctl was found and
812 zero otherwise.
813
814 Note that this builtin is defined by the zsh/compctl module.
815
817 The following additional condition codes for use within the [[ ... ]]
818 construct are available in completion widgets. These work on the spe‐
819 cial parameters. All of these tests can also be performed by the
820 compset builtin, but in the case of the condition codes the contents of
821 the special parameters are not modified.
822
823 -prefix [ number ] pattern
824 true if the test for the -P option of compset would succeed.
825
826 -suffix [ number ] pattern
827 true if the test for the -S option of compset would succeed.
828
829 -after beg-pat
830 true if the test of the -N option with only the beg-pat given
831 would succeed.
832
833 -between beg-pat end-pat
834 true if the test for the -N option with both patterns would suc‐
835 ceed.
836
838 It is possible by use of the -M option of the compadd builtin command
839 to specify how the characters in the string to be completed (referred
840 to here as the command line) map onto the characters in the list of
841 matches produced by the completion code (referred to here as the trial
842 completions). Note that this is not used if the command line contains a
843 glob pattern and the GLOB_COMPLETE option is set or the pattern_match
844 of the compstate special association is set to a non-empty string.
845
846 The match-spec given as the argument to the -M option (see `Completion
847 Builtin Commands' above) consists of one or more matching descriptions
848 separated by whitespace. Each description consists of a letter fol‐
849 lowed by a colon and then the patterns describing which character se‐
850 quences on the line match which character sequences in the trial com‐
851 pletion. Any sequence of characters not handled in this fashion must
852 match exactly, as usual.
853
854 The forms of match-spec understood are as follows. In each case, the
855 form with an upper case initial character retains the string already
856 typed on the command line as the final result of completion, while with
857 a lower case initial character the string on the command line is
858 changed into the corresponding part of the trial completion.
859
860 m:lpat=tpat
861 M:lpat=tpat
862 Here, lpat is a pattern that matches on the command line, corre‐
863 sponding to tpat which matches in the trial completion.
864
865 l:lanchor|lpat=tpat
866 L:lanchor|lpat=tpat
867 l:lanchor||ranchor=tpat
868 L:lanchor||ranchor=tpat
869 b:lpat=tpat
870 B:lpat=tpat
871 These letters are for patterns that are anchored by another pat‐
872 tern on the left side. Matching for lpat and tpat is as for m
873 and M, but the pattern lpat matched on the command line must be
874 preceded by the pattern lanchor. The lanchor can be blank to
875 anchor the match to the start of the command line string; other‐
876 wise the anchor can occur anywhere, but must match in both the
877 command line and trial completion strings.
878
879 If no lpat is given but a ranchor is, this matches the gap be‐
880 tween substrings matched by lanchor and ranchor. Unlike lanchor,
881 the ranchor only needs to match the trial completion string.
882
883 The b and B forms are similar to l and L with an empty anchor,
884 but need to match only the beginning of the word on the command
885 line or trial completion, respectively.
886
887 r:lpat|ranchor=tpat
888 R:lpat|ranchor=tpat
889 r:lanchor||ranchor=tpat
890 R:lanchor||ranchor=tpat
891 e:lpat=tpat
892 E:lpat=tpat
893 As l, L, b and B, with the difference that the command line and
894 trial completion patterns are anchored on the right side. Here
895 an empty ranchor and the e and E forms force the match to the
896 end of the command line or trial completion string.
897
898 x: This form is used to mark the end of matching specifications:
899 subsequent specifications are ignored. In a single standalone
900 list of specifications this has no use but where matching speci‐
901 fications are accumulated, such as from nested function calls,
902 it can allow one function to override another.
903
904 Each lpat, tpat or anchor is either an empty string or consists of a
905 sequence of literal characters (which may be quoted with a backslash),
906 question marks, character classes, and correspondence classes; ordinary
907 shell patterns are not used. Literal characters match only themselves,
908 question marks match any character, and character classes are formed as
909 for globbing and match any character in the given set.
910
911 Correspondence classes are defined like character classes, but with two
912 differences: they are delimited by a pair of braces, and negated
913 classes are not allowed, so the characters ! and ^ have no special
914 meaning directly after the opening brace. They indicate that a range
915 of characters on the line match a range of characters in the trial com‐
916 pletion, but (unlike ordinary character classes) paired according to
917 the corresponding position in the sequence. For example, to make any
918 ASCII lower case letter on the line match the corresponding upper case
919 letter in the trial completion, you can use `m:{a-z}={A-Z}' (however,
920 see below for the recommended form for this). More than one pair of
921 classes can occur, in which case the first class before the = corre‐
922 sponds to the first after it, and so on. If one side has more such
923 classes than the other side, the superfluous classes behave like normal
924 character classes. In anchor patterns correspondence classes also be‐
925 have like normal character classes.
926
927 The standard `[:name:]' forms described for standard shell patterns
928 (see the section FILENAME GENERATION in zshexpn(1)) may appear in cor‐
929 respondence classes as well as normal character classes. The only spe‐
930 cial behaviour in correspondence classes is if the form on the left and
931 the form on the right are each one of [:upper:], [:lower:]. In these
932 cases the character in the word and the character on the line must be
933 the same up to a difference in case. Hence to make any lower case
934 character on the line match the corresponding upper case character in
935 the trial completion you can use `m:{[:lower:]}={[:upper:]}'. Although
936 the matching system does not yet handle multibyte characters, this is
937 likely to be a future extension, at which point this syntax will handle
938 arbitrary alphabets; hence this form, rather than the use of explicit
939 ranges, is the recommended form. In other cases `[:name:]' forms are
940 allowed. If the two forms on the left and right are the same, the
941 characters must match exactly. In remaining cases, the corresponding
942 tests are applied to both characters, but they are not otherwise con‐
943 strained; any matching character in one set goes with any matching
944 character in the other set: this is equivalent to the behaviour of or‐
945 dinary character classes.
946
947 The pattern tpat may also be one or two stars, `*' or `**'. This means
948 that the pattern on the command line can match any number of characters
949 in the trial completion. In this case the pattern must be anchored (on
950 either side); in the case of a single star, the anchor then determines
951 how much of the trial completion is to be included -- only the charac‐
952 ters up to the next appearance of the anchor will be matched. With two
953 stars, substrings matched by the anchor can be matched, too.
954
955 Examples:
956
957 The keys of the options association defined by the parameter module are
958 the option names in all-lower-case form, without underscores, and with‐
959 out the optional no at the beginning even though the builtins setopt
960 and unsetopt understand option names with upper case letters, under‐
961 scores, and the optional no. The following alters the matching rules
962 so that the prefix no and any underscore are ignored when trying to
963 match the trial completions generated and upper case letters on the
964 line match the corresponding lower case letters in the words:
965
966 compadd -M 'L:|[nN][oO]= M:_= M:{[:upper:]}={[:lower:]}' - \
967 ${(k)options}
968
969 The first part says that the pattern `[nN][oO]' at the beginning (the
970 empty anchor before the pipe symbol) of the string on the line matches
971 the empty string in the list of words generated by completion, so it
972 will be ignored if present. The second part does the same for an under‐
973 score anywhere in the command line string, and the third part uses cor‐
974 respondence classes so that any upper case letter on the line matches
975 the corresponding lower case letter in the word. The use of the upper
976 case forms of the specification characters (L and M) guarantees that
977 what has already been typed on the command line (in particular the pre‐
978 fix no) will not be deleted.
979
980 Note that the use of L in the first part means that it matches only
981 when at the beginning of both the command line string and the trial
982 completion. I.e., the string `_NO_f' would not be completed to
983 `_NO_foo', nor would `NONO_f' be completed to `NONO_foo' because of the
984 leading underscore or the second `NO' on the line which makes the pat‐
985 tern fail even though they are otherwise ignored. To fix this, one
986 would use `B:[nN][oO]=' instead of the first part. As described above,
987 this matches at the beginning of the trial completion, independent of
988 other characters or substrings at the beginning of the command line
989 word which are ignored by the same or other match-specs.
990
991 The second example makes completion case insensitive. This is just the
992 same as in the option example, except here we wish to retain the char‐
993 acters in the list of completions:
994
995 compadd -M 'm:{[:lower:]}={[:upper:]}' ...
996
997 This makes lower case letters match their upper case counterparts. To
998 make upper case letters match the lower case forms as well:
999
1000 compadd -M 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' ...
1001
1002 A nice example for the use of * patterns is partial word completion.
1003 Sometimes you would like to make strings like `c.s.u' complete to
1004 strings like `comp.source.unix', i.e. the word on the command line con‐
1005 sists of multiple parts, separated by a dot in this example, where each
1006 part should be completed separately -- note, however, that the case
1007 where each part of the word, i.e. `comp', `source' and `unix' in this
1008 example, is to be completed from separate sets of matches is a differ‐
1009 ent problem to be solved by the implementation of the completion wid‐
1010 get. The example can be handled by:
1011
1012 compadd -M 'r:|.=* r:|=*' \
1013 - comp.sources.unix comp.sources.misc ...
1014
1015 The first specification says that lpat is the empty string, while an‐
1016 chor is a dot; tpat is *, so this can match anything except for the `.'
1017 from the anchor in the trial completion word. So in `c.s.u', the
1018 matcher sees `c', followed by the empty string, followed by the anchor
1019 `.', and likewise for the second dot, and replaces the empty strings
1020 before the anchors, giving `c[omp].s[ources].u[nix]', where the last
1021 part of the completion is just as normal.
1022
1023 With the pattern shown above, the string `c.u' could not be completed
1024 to `comp.sources.unix' because the single star means that no dot
1025 (matched by the anchor) can be skipped. By using two stars as in
1026 `r:|.=**', however, `c.u' could be completed to `comp.sources.unix'.
1027 This also shows that in some cases, especially if the anchor is a real
1028 pattern, like a character class, the form with two stars may result in
1029 more matches than one would like.
1030
1031 The second specification is needed to make this work when the cursor is
1032 in the middle of the string on the command line and the option COM‐
1033 PLETE_IN_WORD is set. In this case the completion code would normally
1034 try to match trial completions that end with the string as typed so
1035 far, i.e. it will only insert new characters at the cursor position
1036 rather than at the end. However in our example we would like the code
1037 to recognise matches which contain extra characters after the string on
1038 the line (the `nix' in the example). Hence we say that the empty
1039 string at the end of the string on the line matches any characters at
1040 the end of the trial completion.
1041
1042 More generally, the specification
1043
1044 compadd -M 'r:|[.,_-]=* r:|=*' ...
1045
1046 allows one to complete words with abbreviations before any of the char‐
1047 acters in the square brackets. For example, to complete veryverylong‐
1048 file.c rather than veryverylongheader.h with the above in effect, you
1049 can just type very.c before attempting completion.
1050
1051 The specifications with both a left and a right anchor are useful to
1052 complete partial words whose parts are not separated by some special
1053 character. For example, in some places strings have to be completed
1054 that are formed `LikeThis' (i.e. the separate parts are determined by a
1055 leading upper case letter) or maybe one has to complete strings with
1056 trailing numbers. Here one could use the simple form with only one an‐
1057 chor as in:
1058
1059 compadd -M 'r:|[[:upper:]0-9]=* r:|=*' LikeTHIS FooHoo 5foo123 5bar234
1060
1061 But with this, the string `H' would neither complete to `FooHoo' nor to
1062 `LikeTHIS' because in each case there is an upper case letter before
1063 the `H' and that is matched by the anchor. Likewise, a `2' would not be
1064 completed. In both cases this could be changed by using `r:|[[:up‐
1065 per:]0-9]=**', but then `H' completes to both `LikeTHIS' and `FooHoo'
1066 and a `2' matches the other strings because characters can be inserted
1067 before every upper case letter and digit. To avoid this one would use:
1068
1069 compadd -M 'r:[^[:upper:]0-9]||[[:upper:]0-9]=** r:|=*' \
1070 LikeTHIS FooHoo foo123 bar234
1071
1072 By using these two anchors, a `H' matches only upper case `H's that are
1073 immediately preceded by something matching the left anchor `[^[:up‐
1074 per:]0-9]'. The effect is, of course, that `H' matches only the string
1075 `FooHoo', a `2' matches only `bar234' and so on.
1076
1077 When using the completion system (see zshcompsys(1)), users can define
1078 match specifications that are to be used for specific contexts by using
1079 the matcher and matcher-list styles. The values for the latter will be
1080 used everywhere.
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.8.1 February 12, 2022 ZSHCOMPWID(1)