Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is possible to completely replace the current word with a single new word. With my bash 4.2.29, I can do this:</p> <pre><code>_xxx() { COMPREPLY=( foo ); } complete -F _xxx x x bar # pressing tab turns this into x foo </code></pre> <p>You encounter problems, however, if there is more than one possible completion, and you want to get partial completion of the common prefix. Then my experiments indicate that bash will try to match the available completions to the prefix you entered.</p> <p>So in general you should probably only replace the current argument with something completely different if that something is uniquely defined. Otherwise, you should generate completions which match the current prefix, to have the user select from those. In your case you could replace the <code>COMPREPLY=($(compgen -P "$dir" -W "${results}"))</code> with something along these lines:</p> <pre><code>local IFS=$'\n' COMPREPLY=( $(find "${dir}" -maxdepth 1 -type d -iname "${cur#*/}*" -printf "%P\n") ) if [[ ${#COMPREPLY[@]} -eq 1 ]]; then COMPREPLY=( "${dir}${COMPREPLY[0]}" ) fi </code></pre> <p>However, in this specific case it might be better to only replace the prefix digit by the appropriate path, and leave everything else to the default bash completion:</p> <pre><code>_up_prefix() { local dir cur="${COMP_WORDS[COMP_CWORD]}" COMPREPLY=() if [[ ${cur} =~ ^[0-9]+/? ]]; then # Starting with a number, possibly followed by a slash dir=$( printf "%${cur%%/*}s" ); dir="${dir// /../}" if [[ ${cur} == */* ]]; then dir="${dir}${cur#*/}" fi COMPREPLY=( "${dir}" "${dir}." ) # hack to suppress trailing space elif [[ ${cur} != */* ]]; then # Not a digit, and no slash either, so search parent directories COMPREPLY=( $(IFS='/'; compgen -W "${PWD}" "${cur}") ) if [[ ${#COMPREPLY[@]} -eq 1 ]]; then dir="${PWD%${COMPREPLY[0]}/*}${COMPREPLY[0]}/" COMPREPLY=( "${dir}" "${dir}." ) # hack as above fi fi } complete -F _up_prefix -o dirnames up </code></pre> <p>The code becomes a lot easier to read and maintain, plus more efficient to boot. The only drawback is that in some cases you'd have to press tab one more time than you used to: once to substitute the prefix, and twice more to actually see the list of possible completions. Your choice whether that's acceptable.</p> <p>One more thing: the completion will turn the argument into a regular path, but your up function as it is does not accept those. So perhaps you should start that function with a <code>[[ -d $1 ]]</code> check, and simply cd to that directory if it exists. Otherwise your completion will generate arguments which are unacceptable to the called function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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