Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The bash documentation can be a little enigmatic. The simplest solution is to alter the completion function binding:</p> <pre><code>complete -o filenames -F _foo foo </code></pre> <p>This means the function returns filenames (including directories), and special handling of the results is enabled.</p> <p>(IMHO the documentation doesn't make it clear that this effectively post-processes <code>COMPREPLY[]</code> as set by your completion function; and that some of the <code>-o</code> options, that one included, when applied to <code>compgen</code> appear to have no effect.)</p> <p>You can get closer to normal bash behaviour by using:</p> <pre><code> complete -o filenames -o bashdefault -F _foo foo </code></pre> <p>that gets you "~" completion back.</p> <p>There are two problems with the above however:</p> <ul> <li>if you have a directory named "encrypt" or "decrypt" then the expansion of your keywords will grow a trailing "/" </li> <li><code>$VARIABLE</code> expansion won't work, <code>$</code> will become <code>\$</code> to better match a filename with a <code>$</code>. Similarly <code>@host</code> expansion won't work.</li> </ul> <p>The only way that I have found to deal with this is to process the <code>compgen</code> output, and not rely on the "filenames" post-processing:</p> <pre><code>function _foo() { local cmds cur ff if [ $COMP_CWORD -eq 1 ] then cur="${COMP_WORDS[1]}" cmds="encrypt decrypt" COMPREPLY=($(compgen -W "${cmds}" -- ${cur}) ) COMPREPLY=( "${COMPREPLY[@]/%/ }" ) # add trailing space to each else # get all matching files and directories COMPREPLY=($(compgen -f -- "${COMP_WORDS[${COMP_CWORD}]}" )) for ((ff=0; ff&lt;${#COMPREPLY[@]}; ff++)); do test -d "${COMPREPLY[$ff]}" &amp;&amp; COMPREPLY[$ff]="${COMPREPLY[$ff]}/" test -f "${COMPREPLY[$ff]}" &amp;&amp; COMPREPLY[$ff]="${COMPREPLY[$ff]} " done fi } complete -o bashdefault -o nospace -F _foo foo </code></pre> <p>(I also removed the superfluous array for <code>cmd</code> in the above, and made <code>compgen</code> more robust and handle spaces or leading dashes in filenames.)</p> <p>The downsides now are that when you get the intermediate completion list (i.e. when you hit <kbd>tab</kbd> twice to show multiple matches) you won't see a trailing / on directories, and since <code>nospace</code> is enabled the <code>~ $ @</code> expansions won't grow a space to cause them to be accepted. </p> <p>In short, I do not believe you can trivially mix-and-match your own completion and the full bash completion behaviour.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload