<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54
     from ./zsh.texi on 13 May 2000 -->

<TITLE>zsh - Programmable Completion</TITLE>
<link href="zsh_19.html" rel=Next>
<link href="zsh_17.html" rel=Previous>
<link href="zsh_toc.html" rel=ToC>

</HEAD>
<BODY>
<p>Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_17.html">previous</A>, <A HREF="zsh_19.html">next</A>, <A HREF="zsh_24.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC65" HREF="zsh_toc.html#TOC65">Programmable Completion</A></H1>
<P>
<A NAME="IDX1018"></A>
<A NAME="IDX1019"></A>
<A NAME="IDX1020"></A>

</P>
<P>
<CODE>compctl [ -CDT ] <VAR>options</VAR> [ <VAR>command</VAR> ... ]</CODE>

</P>
<P>
<CODE>compctl [ -CDT ] <VAR>options</VAR></CODE><BR>
<CODE>        [  -x  <VAR>pattern</VAR>  <VAR>options</VAR> - ... -- ] [ + <VAR>options</VAR> [ -x ... -- ] ... [+] ]</CODE><BR>
<CODE>        [ <VAR>command</VAR> ... ]</CODE>

</P>
<P>
<CODE>compctl -L [ -CDT ] [ <VAR>command</VAR> ... ]</CODE>

</P>
<P>
<CODE>compctl + <VAR>command</VAR> ...</CODE>

</P>
<P>
Control the editor's completion behaviour according to the supplied set
of <VAR>options</VAR>.  Various editing commands, notably 
<CODE>expand-or-complete-word</CODE>, usually bound to <KBD>TAB</KBD>, will attempt to 
complete a word typed by the user, while others, notably 
<CODE>delete-char-or-list</CODE>, usually bound to <KBD>^D</KBD> in emacs editing mode,
list the possibilities; <CODE>compctl</CODE> controls what those possibilities 
are.  They may for example be filenames (the most common case, and hence the
default), shell variables, or words from a user-specified list.

</P>



<H2><A NAME="SEC66" HREF="zsh_toc.html#TOC66">Command Flags</A></H2>

<P>
Completion of the arguments of a command may be different for each command 
or may use the default.  The behaviour when completing the command word itself
may also be separately specified.  These correspond to the following flags
and arguments, all of which (except for <SAMP>`-L'</SAMP>) may be combined with any
combination of the options described subsequently in section <A HREF="zsh_18.html#SEC67">Options Flags</A>.

</P>
<DL COMPACT>

<DT><CODE><VAR>command</VAR> ...</CODE>
<DD>
controls completion for the named commands, which must be listed last on the
command line.  If completion is attempted for a command with a pathname 
containing slashes and no completion definition is found, the  search is 
retried with the last pathname component.  Note that aliases are expanded
before the command name is determined unless the <CODE>COMPLETE_ALIASES</CODE>
option is set.  Commands should not be combined with the <SAMP>`-D'</SAMP>, 
<SAMP>`-C'</SAMP> or <SAMP>`-T'</SAMP> flags.

<DT><CODE>-D</CODE>
<DD>
controls default completion behavior for the arguments of commands not
assigned any special behavior.  If no <CODE>compctl -D</CODE> command has
been issued, filenames are completed.

<DT><CODE>-C</CODE>
<DD>
controls completion when the command word itself is being completed.
If no <CODE>compctl -C</CODE> command has been issued,  the names of any
executable command (whether in the path or specific to the shell, such
as aliases or functions) are completed.

<DT><CODE>-T</CODE>
<DD>
supplies completion flags to be used  before any other processing is done,
even those given to specific commands with other <CODE>compctl</CODE> 
definitions.  This works only when combined with the <SAMP>`-x'</SAMP> flag
(see section <A HREF="zsh_18.html#SEC72">Extended Completion</A>).
Using the <SAMP>`-T'</SAMP> flag you can define default behaviour which will apply 
to all commands without exception, or you can alter the standard behaviour 
for all commands.  For example, if your access to the user database is too
slow  and/or it contains too many users (so that completion after <CODE>~</CODE>
is too slow to be usable), you can use

<CODE>compctl -Tx  'C[0,*/*]' -f - 's[~]' -k <VAR>friends</VAR> -S/</CODE>

to complete the strings in the array <VAR>friends</VAR> after a <CODE>~</CODE>.  The 
first argument is necessary so that this form of <CODE>~</CODE>-completion is not
tried after the directory name is finished.

<DT><CODE>-L</CODE>
<DD>
lists the existing completion behaviour in a manner suitable for putting 
into a start-up script; the existing behaviour is not changed.  Any 
combination of the above forms may be specified, otherwise all defined 
completions are listed.  Any other flags supplied are ignored.

<DT><CODE>no argument</CODE>
<DD>
If no argument is given, <CODE>compctl</CODE> lists all defined completions in an 
abbreviated form; with a list of <VAR>options</VAR>, all completions with those 
flags set (not counting extended completion) are listed.

If the <CODE>+</CODE> flag is alone and followed immediately by the <VAR>command</VAR>
list, the completion behaviour for all the commands in the list is reset to 
the default.  In other words, completion will subsequently use the options 
specified by the <SAMP>`-D'</SAMP> flag.
</DL>



<H2><A NAME="SEC67" HREF="zsh_toc.html#TOC67">Options Flags</A></H2>

<P>
<CODE>[ -fcFBdeaRGovNAIOPZEnbjrzu ]</CODE><BR>
<CODE>[ -k <VAR>array</VAR> ] [ -g <VAR>globstring</VAR> ] [ -s <VAR>subststring</VAR> ]</CODE><BR>
<CODE>[ -K <VAR>function</VAR> ] [ -H <VAR>num</VAR> <VAR>pattern</VAR> ]</CODE><BR>
<CODE>[ -Q ] [ -P <VAR>prefix</VAR> ] [ -S <VAR>suffix</VAR> ]</CODE><BR>
<CODE>[ -q ] [ -X <VAR>explanation</VAR> ]</CODE><BR>
<CODE>[ -l <VAR>cmd</VAR> ] [ -U ]</CODE>

</P>
<P>
The remaining options specify the type of command arguments to look for 
during completion.  Any combination of these flags may be specified; 
the result is a sorted list of all the possibilities.  The options are 
described in the following sections.

</P>



<H3><A NAME="SEC68" HREF="zsh_toc.html#TOC68">Simple Flags</A></H3>

<P>
These produce completion lists made up by the shell itself:

</P>
<DL COMPACT>

<DT><CODE>-f</CODE>
<DD>
Filenames and file-system paths.

<DT><CODE>-c</CODE>
<DD>
Command names, including aliases, shell functions, builtins and reserved 
words.

<DT><CODE>-F</CODE>
<DD>
Function names.

<DT><CODE>-B</CODE>
<DD>
Names of builtin commands.

<DT><CODE>-m</CODE>
<DD>
Names of external commands.

<DT><CODE>-w</CODE>
<DD>
Reserved words.

<DT><CODE>-a</CODE>
<DD>
Alias names.

<DT><CODE>-R</CODE>
<DD>
Names of regular (non-global) aliases.

<DT><CODE>-G</CODE>
<DD>
Names of global aliases.

<DT><CODE>-d</CODE>
<DD>
This can be combined with <SAMP>`-F'</SAMP>, <SAMP>`-B'</SAMP>, <SAMP>`-w'</SAMP>, <SAMP>`-a'</SAMP>,
<SAMP>`-R'</SAMP> and <SAMP>`-G'</SAMP> to get names of disabled functions, builtins, reserved
words or aliases.

<DT><CODE>-e</CODE>
<DD>
This option (to show enabled commands) is in effect by default, but
may be combined with <SAMP>`-d'</SAMP>; <SAMP>`-de'</SAMP> in combination with
<SAMP>`-F'</SAMP>, <SAMP>`-B'</SAMP>, <SAMP>`-w'</SAMP>, <SAMP>`-a'</SAMP>, <SAMP>`-R'</SAMP> and <SAMP>`-G'</SAMP>
will complete names of functions, builtins, reserved words or aliases
whether or not they are disabled.

<DT><CODE>-o</CODE>
<DD>
Names of shell options. See section <A HREF="zsh_16.html#SEC57">Options</A>.
 
<DT><CODE>-v</CODE>
<DD>
Names of any variable defined in the shell.

<DT><CODE>-N</CODE>
<DD>
Names of scalar (non-array) parameters.

<DT><CODE>-A</CODE>
<DD>
Array names.

<DT><CODE>-I</CODE>
<DD>
Names of integer variables.

<DT><CODE>-O</CODE>
<DD>
Names of read-only variables.

<DT><CODE>-p</CODE>
<DD>
Names of parameters used by the shell (including special parameters).

<DT><CODE>-Z</CODE>
<DD>
Names of shell special parameters.

<DT><CODE>-E</CODE>
<DD>
Names of environment variables.

<DT><CODE>-n</CODE>
<DD>
Named directories.

<DT><CODE>-b</CODE>
<DD>
Key binding names.

<DT><CODE>-j</CODE>
<DD>
Job names: the first word of the job leader's command line.  This is useful 
with the kill builtin.

<DT><CODE>-r</CODE>
<DD>
Names of running jobs.

<DT><CODE>-z</CODE>
<DD>
Names of suspended jobs.

<DT><CODE>-u</CODE>
<DD>
User names.
</DL>



<H3><A NAME="SEC69" HREF="zsh_toc.html#TOC69">Flags with arguments</A></H3>

<P>
These have user supplied arguments to determine how the list of completions
is to be made up:

</P>
<DL COMPACT>

<DT><CODE>-k <VAR>array</VAR></CODE>
<DD>
Names taken from the elements of <CODE>$<VAR>array</VAR></CODE> (note that the <CODE>$</CODE>
does not appear on the command  line).   Alternatively, the argument 
<CODE><VAR>array</VAR></CODE> itself may be a set of space or comma separated values
in parentheses, in which any delimiter may be escaped with a backslash;
in this case the argument should be quoted.  For example,
<SAMP>`compctl -k "(cputime filesize datasize stacksize coredumpsize
resident descriptors)" limit'</SAMP>.

<DT><CODE>-g <VAR>globstring</VAR></CODE>
<DD>
The <VAR>globstring</VAR> is expanded using filename globbing; it should 
be quoted to protect it from immediate expansion.  The resulting filenames
are taken as possible completions.  Use <CODE>*(/)</CODE> instead of <CODE>*/</CODE> 
for directories.  The <CODE>fignore</CODE> special parameter is not applied to 
the resulting files.  More than one pattern may be given separated by 
blanks. (Note that brace expansion is not part of globbing.  Use the syntax
(<CODE>either|or</CODE>) to match alternatives.)

<DT><CODE>-s <VAR>subststring</VAR></CODE>
<DD>
The <VAR>subststring</VAR> is split into words and these words are than
expanded using all shell expansion mechanisms (see the <CODE>zshexpn</CODE>
manual page). The resulting words are taken as possible
completions. The <CODE>fignore</CODE> special parameter is not applied to the
resulting files.  Note that <SAMP>`-g'</SAMP> is faster for filenames.

<DT><CODE>-K <VAR>function</VAR></CODE>
<DD>
Call the given function to get the completions.  The function is passed
two arguments: the prefix and the suffix of the word on which completion is
to be attempted, in other words those characters before the cursor position,
and those from the cursor position onwards.  The function should set the 
variable <CODE>reply</CODE> to an array containing the completions (one completion 
per element); note that <CODE>reply</CODE> should not be made local to the
function.  From such a function the command line can be accessed with the
<SAMP>`-c'</SAMP> and <SAMP>`-l'</SAMP> flags to the <CODE>read</CODE> builtin.  For example, 

<CODE>function whoson { reply=(`users`); }</CODE><BR>
<CODE>compctl -K whoson talk</CODE>

completes only logged-on users after <SAMP>`talk'</SAMP>.  Note that <CODE>whoson</CODE> 
must return an array so that just <CODE>reply=`users`</CODE> is incorrect.

<DT><CODE>-H <VAR>num pattern</VAR></CODE>
<DD>
The possible completions are taken from the last <VAR>num</VAR> history
lines.  Only words matching <VAR>pattern</VAR> are taken.  If <VAR>num</VAR> is
zero or negative the whole history is searched and if <VAR>pattern</VAR> is
the empty string all words are taken (as with <CODE>*</CODE>).  A typical
use is


<PRE>
compctl -D -f + -H 0 '' -X '(No file found; using history)'
</PRE>

which forces completion to look back in the history list for a word if no
filename matches.  The explanation string is useful as it tells the user
that no file of that name exists, which is otherwise ambiguous.  (See the
next section for <SAMP>`-X'</SAMP>.)
</DL>



<H3><A NAME="SEC70" HREF="zsh_toc.html#TOC70">Control Flags</A></H3>

<P>
These do not directly specify types of name to be completed, but manipulate
the options that do:

</P>
<DL COMPACT>

<DT><CODE>-Q</CODE>
<DD>
This instructs the shell not to quote any metacharacters in the possible 
completions.  Normally the results of a completion are inserted into the 
command line with any metacharacters quoted so that they  are interpreted
as  normal characters.  This is appropriate for filenames and ordinary
strings.  However, for special effects, such as inserting a backquoted 
expression from a completion array (<SAMP>`-k'</SAMP>) so that the expression will 
not be evaluated until the complete line is executed, this option must be 
used.

<DT><CODE>-P <VAR>prefix</VAR></CODE>
<DD>
The <VAR>prefix</VAR> is inserted just before the completed string; any initial 
part already typed will be completed and the whole <VAR>prefix</VAR> ignored for 
completion purposes.  For example, 

<CODE>compctl -j -P "%" kill</CODE> 

inserts a <CODE>%</CODE> after the <CODE>kill</CODE> command and then completes job 
names.

<DT><CODE>-S <VAR>suffix</VAR></CODE>
<DD>
When a completion is found the <VAR>suffix</VAR> is inserted after the completed
string.  In the case of menu completion the <VAR>suffix</VAR> is inserted 
immediately, but it is still possible to cycle through the list of 
completions by repeatedly hitting the same key.

<DT><CODE>-q</CODE>
<DD>
If used with a suffix as specified by the previous option, this causes the 
suffix to be removed if the next character typed is a blank or does not 
insert anything (the same rule as used for the <CODE>AUTO_REMOVE_SLASH</CODE>
option).  The option is most useful for list separators (comma, colon, etc.).

<DT><CODE>-l <VAR>cmd</VAR></CODE>
<DD>
This option cannot be combined with any other option.  It
restricts the range of command line words that are considered to be
arguments.  If combined with one of the extended completion patterns
<SAMP>`p[...]'</SAMP>, <SAMP>`r[...]'</SAMP>, or <SAMP>`R[...]'</SAMP> 
(see section <A HREF="zsh_18.html#SEC72">Extended Completion</A>).
the range is restricted to the arguments specified in the brackets.
Completion is then performed as if these had been given as arguments to the
<VAR>cmd</VAR> supplied with the option.  If the <VAR>cmd</VAR> string is empty the
first word in the range is instead taken as the command name, and command
name completion performed on the first word in the range.  For example,


<PRE>
compctl -x 'r[-exec,;]' -l '' -- find
</PRE>

completes arguments between <CODE>-exec</CODE> and the following <CODE>;</CODE> (or the
end of the command line if there is no such string) as if they were a separate
command line.

<DT><CODE>-U</CODE>
<DD>
Use the whole list of possible completions, whether or not they actually
match the word on the command line.  The word typed so far will be
deleted.  This is most useful with a function (given by the <SAMP>`-K'</SAMP> option)
which can examine the word components passed to it (or via the <CODE>read</CODE> 
builtin's <SAMP>`-c'</SAMP> and <SAMP>`-l'</SAMP> flags) and use its own criteria to decide 
what matches.  If there is no completion, the original word is retained.

<DT><CODE>-X <VAR>explanation</VAR></CODE>
<DD>
Print <VAR>explanation</VAR> when trying completion on the current set of 
options.  A <CODE>%n</CODE> in this string is replaced by the number of 
matches.
</DL>



<H2><A NAME="SEC71" HREF="zsh_toc.html#TOC71">Alternative Completion</A></H2>

<P>
<CODE>compctl [ -CDT ] <VAR>options</VAR> + <VAR>options</VAR> [ + ... ] [ + ] <VAR>command</VAR> ...</CODE>

</P>
<P>
The form with <CODE>+</CODE> specifies alternative <VAR>options</VAR>.  Completion is
tried with the <VAR>options</VAR> before the first <CODE>+</CODE>.  If this produces no
matches completion is tried with the flags after the <CODE>+</CODE> and so on.  If
there are no flags after the last <CODE>+</CODE> and a match has not been found up
to that point, default completion is tried.

</P>



<H2><A NAME="SEC72" HREF="zsh_toc.html#TOC72">Extended Completion</A></H2>

<P>
<CODE>compctl  [  -CDT  ]  <VAR>options</VAR> -x <VAR>pattern</VAR> <VAR>options</VAR> - ... -- [ <VAR>command</VAR> ... ]</CODE>

</P>
<P>
<CODE>compctl [ -CDT ] <VAR>options</VAR> [ -x <VAR>pattern</VAR> <VAR>options</VAR> - ... -- ]</CODE><BR>
<CODE>        [ + <VAR>options</VAR> [ -x ... -- ] ... [+] ] [ <VAR>command</VAR> ... ]</CODE>

</P>
<P>
The form with <SAMP>`-x'</SAMP> specifies extended completion for the commands 
given; as shown, it may be combined with alternative completion using
<CODE>+</CODE>.  Each <VAR>pattern</VAR> is examined in turn; when a match is
found, the corresponding <VAR>options</VAR>, as described in
section <A HREF="zsh_18.html#SEC67">Options Flags</A>, are used to generate possible completions.  If no
<VAR>pattern</VAR> matches, the <VAR>options</VAR> given before the 
<SAMP>`-x'</SAMP> are used.

</P>
<P>
Note that each pattern should be supplied as a single argument and should be
quoted to prevent expansion of meta-characters by the shell.

</P>
<P>
A <VAR>pattern</VAR> is built of sub-patterns separated by commas; it
matches if at least one of these sub-patterns matches (they are 
<CODE>or</CODE>'ed).  
These sub-patterns are in turn composed of other sub-patterns separated by
white spaces which match if all of the sub-patterns match (they are 
<CODE>and</CODE>'ed).  An element of the sub-patterns is of the  form 
<CODE>c[...][...]</CODE>, where the pairs of brackets may be repeated as
often as necessary, and matches if any of the sets of brackets match 
(an <CODE>or</CODE>).  The example below makes this clearer.

</P>
<P>
The elements may be any of the following:

</P>
<DL COMPACT>

<DT><CODE><VAR>s</VAR>[<VAR>string</VAR>] ...</CODE>
<DD>
Matches if the current word on the command line starts with one
of the strings given in brackets.  The <VAR>string</VAR> is not removed and is not
part of the completion.

<DT><CODE><VAR>S</VAR>[<VAR>string</VAR>] ...</CODE>
<DD>
Like <VAR>s</VAR>[<VAR>string</VAR>] except that the <VAR>string</VAR> is part of the 
completion.

<DT><CODE><VAR>p</VAR>[<VAR>from</VAR>,<VAR>to</VAR>] ...</CODE>
<DD>
Matches if the number of the current word is between one of
the <VAR>from</VAR> and <VAR>to</VAR> pairs inclusive.  The comma and <VAR>to</VAR> are 
optional; <VAR>to</VAR> defaults to the same value as <VAR>from</VAR>.  The numbers 
may be negative: <SAMP>`-n'</SAMP> refers to the <CODE>n</CODE>'th last word on the 
line.

<DT><CODE><VAR>c</VAR>[<VAR>offset</VAR>,<VAR>string</VAR>] ...</CODE>
<DD>
Matches if the <VAR>string</VAR> matches the word offset by
<VAR>offset</VAR> from the current word position.  Usually <VAR>offset</VAR> will 
be negative.

<DT><CODE><VAR>C</VAR>[<VAR>offset</VAR>,<VAR>pattern</VAR>] ...</CODE>
<DD>
Like <VAR>c</VAR> but using pattern matching instead.

<DT><CODE><VAR>w</VAR>[<VAR>index</VAR>,<VAR>string</VAR>] ...</CODE>
<DD>
Matches if the word in position <VAR>index</VAR> is equal to the 
corresponding  <VAR>string</VAR>.  Note that the word count is made after any
alias expansion.

<DT><CODE><VAR>W</VAR>[<VAR>index</VAR>,<VAR>pattern</VAR>] ...</CODE>
<DD>
Like <VAR>w</VAR> but using pattern matching instead.

<DT><CODE><VAR>n</VAR>[<VAR>index</VAR>,<VAR>string</VAR>] ...</CODE>
<DD>
Matches if the current word contains <VAR>string</VAR>.  Anything up to and 
including the <VAR>index</VAR>'th occurrence of this <VAR>string</VAR> will not be
considered part of the completion, but the rest will.  <VAR>index</VAR> may be
negative to count from the end: in most cases, <VAR>index</VAR> will be 1 or 
-1.

<DT><CODE><VAR>N</VAR>[<VAR>index</VAR>,<VAR>string</VAR>] ...</CODE>
<DD>
Like <VAR>n</VAR>[<VAR>index</VAR>,<VAR>string</VAR>] except that the <VAR>string</VAR>
will be taken as a character class.  Anything up to and including the 
<VAR>index</VAR>'th occurrence of any of the characters in <VAR>string</VAR> will not
be considered part of the completion.

<DT><CODE><VAR>m</VAR>[<VAR>min</VAR>,<VAR>max</VAR>] ...</CODE>
<DD>
Matches if the total number of words lies between <VAR>min</VAR> and <VAR>max</VAR> 
inclusive.

<DT><CODE><VAR>r</VAR>[<VAR>str1</VAR>,<VAR>str2</VAR>] ...</CODE>
<DD>
Matches if the cursor is after a word with prefix <VAR>str1</VAR>.  If there is 
also a word with prefix <VAR>str2</VAR> on the command line it matches only if 
the cursor is before this word.

<DT><CODE><VAR>R</VAR>[<VAR>str1</VAR>,<VAR>str2</VAR>] ...</CODE>
<DD>
Like <VAR>r</VAR> but using pattern matching instead.
</DL>



<H2><A NAME="SEC73" HREF="zsh_toc.html#TOC73">Example</A></H2>

<P>
<CODE>compctl -u -x 's[+] c[-1,-f],s[-f+]' -g '~/Mail/*(:t)' - 's[-f],c[-1,-f]' -f -- mail</CODE>

</P>
<P>
This is to be interpreted as follows:

</P>
<P>
If the current command is <CODE>mail</CODE>, then

</P>
<P>
if  ((the current word begins with <CODE>+</CODE> and the previous word is 
<CODE>-f</CODE>) or (the current word begins with <CODE>-f+</CODE>)), then complete the 
non-directory part (the <CODE>:t</CODE> glob modifier) of files in the directory  
<TT>`~/Mail'</TT>;
else

</P>
<P>
if the current word begins with <SAMP>`-f'</SAMP> or the previous word was 
<SAMP>`-f'</SAMP>, then complete any file; else

</P>
<P>
complete user names.

</P>

<P><HR><P>
<p>Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_17.html">previous</A>, <A HREF="zsh_19.html">next</A>, <A HREF="zsh_24.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>.
</BODY>
</HTML>
