diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/bash/a-programmable-completion-example.html | |
new repository
Diffstat (limited to 'devdocs/bash/a-programmable-completion-example.html')
| -rw-r--r-- | devdocs/bash/a-programmable-completion-example.html | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/devdocs/bash/a-programmable-completion-example.html b/devdocs/bash/a-programmable-completion-example.html new file mode 100644 index 00000000..5d00faf6 --- /dev/null +++ b/devdocs/bash/a-programmable-completion-example.html @@ -0,0 +1,57 @@ +<h1 class="section">A Programmable Completion Example</h1> <p>The most common way to obtain additional completion functionality beyond the default actions <code>complete</code> and <code>compgen</code> provide is to use a shell function and bind it to a particular command using <code>complete -F</code>. </p> <p>The following function provides completions for the <code>cd</code> builtin. It is a reasonably good example of what shell functions must do when used for completion. This function uses the word passed as <code>$2</code> to determine the directory name to complete. You can also use the <code>COMP_WORDS</code> array variable; the current word is indexed by the <code>COMP_CWORD</code> variable. </p> <p>The function relies on the <code>complete</code> and <code>compgen</code> builtins to do much of the work, adding only the things that the Bash <code>cd</code> does beyond accepting basic directory names: tilde expansion (see <a href="tilde-expansion">Tilde Expansion</a>), searching directories in <var>$CDPATH</var>, which is described above (see <a href="bourne-shell-builtins">Bourne Shell Builtins</a>), and basic support for the <code>cdable_vars</code> shell option (see <a href="the-shopt-builtin">The Shopt Builtin</a>). <code>_comp_cd</code> modifies the value of <var>IFS</var> so that it contains only a newline to accommodate file names containing spaces and tabs – <code>compgen</code> prints the possible completions it generates one per line. </p> <p>Possible completions go into the <var>COMPREPLY</var> array variable, one completion per array element. The programmable completion system retrieves the completions from there when the function returns. </p> <div class="example"> <pre class="example"># A completion function for the cd builtin +# based on the cd completion function from the bash_completion package +_comp_cd() +{ + local IFS=$' \t\n' # normalize IFS + local cur _skipdot _cdpath + local i j k + + # Tilde expansion, which also expands tilde to full pathname + case "$2" in + \~*) eval cur="$2" ;; + *) cur=$2 ;; + esac + + # no cdpath or absolute pathname -- straight directory completion + if [[ -z "${CDPATH:-}" ]] || [[ "$cur" == @(./*|../*|/*) ]]; then + # compgen prints paths one per line; could also use while loop + IFS=$'\n' + COMPREPLY=( $(compgen -d -- "$cur") ) + IFS=$' \t\n' + # CDPATH+directories in the current directory if not in CDPATH + else + IFS=$'\n' + _skipdot=false + # preprocess CDPATH to convert null directory names to . + _cdpath=${CDPATH/#:/.:} + _cdpath=${_cdpath//::/:.:} + _cdpath=${_cdpath/%:/:.} + for i in ${_cdpath//:/$'\n'}; do + if [[ $i -ef . ]]; then _skipdot=true; fi + k="${#COMPREPLY[@]}" + for j in $( compgen -d -- "$i/$cur" ); do + COMPREPLY[k++]=${j#$i/} # cut off directory + done + done + $_skipdot || COMPREPLY+=( $(compgen -d -- "$cur") ) + IFS=$' \t\n' + fi + + # variable names if appropriate shell option set and no completions + if shopt -q cdable_vars && [[ ${#COMPREPLY[@]} -eq 0 ]]; then + COMPREPLY=( $(compgen -v -- "$cur") ) + fi + + return 0 +} +</pre> +</div> <p>We install the completion function using the <samp>-F</samp> option to <code>complete</code>: </p> <div class="example"> <pre class="example"># Tell readline to quote appropriate and append slashes to directories; +# use the bash default completion for other arguments +complete -o filenames -o nospace -o bashdefault -F _comp_cd cd +</pre> +</div> <p>Since we’d like Bash and Readline to take care of some of the other details for us, we use several other options to tell Bash and Readline what to do. The <samp>-o filenames</samp> option tells Readline that the possible completions should be treated as filenames, and quoted appropriately. That option will also cause Readline to append a slash to filenames it can determine are directories (which is why we might want to extend <code>_comp_cd</code> to append a slash if we’re using directories found via <var>CDPATH</var>: Readline can’t tell those completions are directories). The <samp>-o nospace</samp> option tells Readline to not append a space character to the directory name, in case we want to append to it. The <samp>-o bashdefault</samp> option brings in the rest of the "Bash default" completions – possible completions that Bash adds to the default Readline set. These include things like command name completion, variable completion for words beginning with ‘<samp>$</samp>’ or ‘<samp>${</samp>’, completions containing pathname expansion patterns (see <a href="filename-expansion">Filename Expansion</a>), and so on. </p> <p>Once installed using <code>complete</code>, <code>_comp_cd</code> will be called every time we attempt word completion for a <code>cd</code> command. </p> <p>Many more examples – an extensive collection of completions for most of the common GNU, Unix, and Linux commands – are available as part of the bash_completion project. This is installed by default on many GNU/Linux distributions. Originally written by Ian Macdonald, the project now lives at <a href="https://github.com/scop/bash-completion/">https://github.com/scop/bash-completion/</a>. There are ports for other systems such as Solaris and Mac OS X. </p> <p>An older version of the bash_completion package is distributed with bash in the <samp>examples/complete</samp> subdirectory. </p> <div class="_attribution"> + <p class="_attribution-p"> + Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br> + <a href="https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html</a> + </p> +</div> |
