Debian Codegraph @

TABEXP.sgml

raw
1 <title>An Introduction to Programmable Completion</title>
2
3 <para>The <firstterm>programmable completion</firstterm> feature in
4 Bash permits typing a partial command, then pressing the
5 <keycap>[Tab]</keycap> key to auto-complete the command sequence.
6
7 <footnote><para>This works only from the <firstterm>command
8 line</firstterm>, of course, and not within a
9 script.</para></footnote>
10
11 If multiple completions are possible, then <keycap>[Tab]</keycap>
12 lists them all. Let's see how it works.</para>
13
14 <para>
15 <screen>
16 <prompt>bash$ </prompt><userinput>xtra[Tab]</userinput>
17 <computeroutput>xtraceroute xtrapin xtrapproto
18 xtraceroute.real xtrapinfo xtrapreset
19 xtrapchar xtrapout xtrapstats</computeroutput>
20
21
22 <prompt>bash$ </prompt><userinput>xtrac[Tab]</userinput>
23 <computeroutput>xtraceroute xtraceroute.real</computeroutput>
24
25
26 <prompt>bash$ </prompt><userinput>xtraceroute.r[Tab]</userinput>
27 <computeroutput>xtraceroute.real</computeroutput>
28 </screen>
29 </para>
30
31 <para>Tab completion also works for variables and path names.</para>
32
33 <para>
34 <screen>
35 <prompt>bash$ </prompt><userinput>echo $BASH[Tab]</userinput>
36 <computeroutput>$BASH $BASH_COMPLETION $BASH_SUBSHELL
37 $BASH_ARGC $BASH_COMPLETION_DIR $BASH_VERSINFO
38 $BASH_ARGV $BASH_LINENO $BASH_VERSION
39 $BASH_COMMAND $BASH_SOURCE</computeroutput>
40
41
42 <prompt>bash$ </prompt><userinput>echo /usr/local/[Tab]</userinput>
43 <computeroutput>bin/ etc/ include/ libexec/ sbin/ src/
44 doc/ games/ lib/ man/ share/</computeroutput>
45 </screen>
46 </para>
47
48
49 <para><anchor id="completeref"></para>
50
51 <para>The Bash <command>complete</command> and
52 <command>compgen</command> <link
53 linkend="builtinref">builtins</link> make it
54 possible for <firstterm>tab completion</firstterm> to
55 recognize partial <firstterm>parameters</firstterm> and
56 <firstterm>options</firstterm> to commands. In a very simple case,
57 we can use <command>complete</command> from the command-line to
58 specify a short list of acceptable parameters.</para>
59
60
61 <screen>
62 <prompt>bash$ </prompt><userinput>touch sample_command</userinput>
63 <prompt>bash$ </prompt><userinput>touch file1.txt file2.txt file2.doc file30.txt file4.zzz</userinput>
64 <prompt>bash$ </prompt><userinput>chmod +x sample_command</userinput>
65 <prompt>bash$ </prompt><userinput>complete -f -X '!*.txt' sample_command</userinput>
66
67
68 <prompt>bash$ </prompt><userinput>./sample[Tab][Tab]</userinput>
69 <computeroutput>sample_command</computeroutput>
70 <computeroutput>file1.txt file2.txt file30.txt</computeroutput>
71 </screen>
72
73 <para>The <option>-f</option> option to
74 <firstterm>complete</firstterm> specifies filenames,
75 and <option>-X</option> the filter pattern.</para>
76
77 <para><anchor id="compgenref"></para>
78
79 <para>For anything more complex, we could write a script that
80 specifies a list of acceptable command-line parameters.
81 The <command>compgen</command> builtin expands a list of
82 <firstterm>arguments</firstterm> to <firstterm>generate</firstterm>
83 completion matches. </para>
84
85 <para>Let us take a <link linkend="usegetopt2">modified version</link>
86 of the <emphasis>UseGetOpt.sh</emphasis> script as an example
87 command. This script accepts a number of command-line parameters,
88 preceded by either a single or double dash. And here is the
89 corresponding <firstterm>completion script</firstterm>, by
90 convention given a filename corresponding to its associated
91 command.</para>
92
93 <example id="usegetoptex">
94 <title>Completion script for
95 <firstterm>UseGetOpt.sh</firstterm></title>
96 <programlisting>&usegetoptex;</programlisting>
97 </example>
98
99 <para>Now, let's try it.</para>
100
101 <screen>
102 <prompt>bash$ </prompt><userinput>source UseGetOpt-2</userinput>
103
104 <prompt>bash$ </prompt><userinput>./UseGetOpt-2.sh -[Tab]</userinput>
105 <computeroutput>-- --aoption --debug --file --help --log --test
106 -a -d -f -h -l -t</computeroutput>
107
108
109 <prompt>bash$ </prompt><userinput>./UseGetOpt-2.sh --[Tab]</userinput>
110 <computeroutput>-- --aoption --debug --file --help --log --test</computeroutput>
111 </screen>
112
113
114 <para>
115 We begin by <link linkend="sourceref">sourcing</link> the <quote>completion
116 script.</quote> This sets the command-line parameters.
117 <footnote><para>Normally the default parameter completion files reside
118 in either the <filename class="directory">/etc/profile.d</filename>
119 directory or in <filename
120 class="directory">/etc/bash_completion</filename>. These autoload on
121 system startup. So, after writing a useful completion script, you
122 might wish to move it (as <firstterm>root</firstterm>, of course)
123 to one of these directories.</para></footnote>
124 </para>
125
126 <para>In the first instance, hitting <keycap>[Tab]</keycap> after
127 a single dash, the output is all the possible parameters preceded by
128 <emphasis>one or more</emphasis> dashes. Hitting <keycap>[Tab]</keycap>
129 after <emphasis>two</emphasis> dashes gives the possible parameters
130 preceded by <emphasis>two or more</emphasis> dashes.</para>
131
132
133 <para>Now, just what is the point of having to jump through flaming
134 hoops to enable command-line tab completion? <emphasis>It saves
135 keystrokes.</emphasis>
136 <footnote><para>It has been extensively documented that
137 programmers are willing to put in long hours of effort in
138 order to save ten minutes of <quote>unnecessary</quote>
139 labor. This is known as
140 <firstterm>optimization</firstterm>.</para></footnote>
141 </para>
142
143
144 <para>--</para>
145
146 <para><emphasis>Resources:</emphasis></para>
147
148 <para>Bash <ulink url="http://freshmeat.net/projects/bashcompletion">
149 programmable completion</ulink> project</para>
150
151 <para>Mitch Frazier's <ulink
152 url="http://www.linuxjournal.com"><citetitle
153 pubwork="journal">Linux Journal</citetitle></ulink> article, <ulink
154 url="http://www.linuxjournal.com/content/more-using-bash-complete-command"><emphasis>More
155 on Using the Bash Complete Command</emphasis></ulink></para>
156
157 <para>Steve's excellent two-part article, <quote>An Introduction to Bash
158 Completion</quote>:
159
160 <ulink
161 url="http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_1">Part
162 1</ulink> and
163
164 <ulink url="http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_2">Part 2</ulink></para>
165