I wrote two functions push and pull to copy files to and from a directory ($FILE_EXCHANGE_DIR). I want bash to autocomplete names from that directory when I use the pull function. I want the autocompletion to only take names from the said directory. In the current form, my autocompletion also finds names of the files in the current directory, regardless of whether there was a match in the source dir ($FILE_EXCHANGE_DIR) or not.
Here are two versions of the function I passed to complete. Neither of them worked.
_pull () {
local cmd="${1##*/}";
local word=${COMP_WORDS[COMP_CWORD]};
local line=${COMP_LINE};
filelist=( $(ls ${FILE_EXCHANGE_DIR} ) )
local TEMP_COMPREPLY=( $(compgen -W "${filelist[@]}" -- ${word} ) )
COMPREPLY=( "${TEMP_COMPREPLY[@]}" )
return 0;
}
_pull ()
{
local word=${COMP_WORDS[COMP_CWORD]};
local line=${COMP_LINE};
local pat="$FILE_EXCHANGE_DIR/*"
COMPREPLY=($(compgen -f -G "$pat" -- "$FILE_EXCHANGE_DIR/${word}"));
i=0
for item in "${COMPREPLY[@]}"; do
COMPREPLY[$i]="${item##*/}"
i+=1
done;
return 0;
}
In the end, I do
complete -f -F _pull pull
Is there a good way of completing names only from one directory (that is not the current one) and not from current? Even in the case when there was no match in the source (which should default to readline default completion?