I have a bash autocompletion function _customcomplete
that I want to apply on a script called run.sh
. If I call complete -F _customcomplete ./run.sh
, then the autocompletion script works fine as long as I am in the same directory and call it with ./run.sh
. However, if I go one level up and call it with ./scripts/run.sh
, the autocompletion script does not work. More importantly, if I am in a different directory with another script called run.sh
, the autocompletion script works on that one when it shouldn't.
In short, is it possible to tie a bash autocompletion function to a specific executable, rather than an invocation string?
Here is a full simplified example:
In completion.sh
is a very simple completion script that always returns three options: one two three
:
_customcomplete() {
local word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "one two three" -- "$word") )
}
I can source it with source completion.sh
and can verify that it sourced with type _customcomplete
:
_customcomplete is a function
_customcomplete() {
local word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "one two three" -- "$word") )
}
In a directory, I have two folders scripts
and other
, both of which contain a script called run.sh
. Each just echos the command line arguments along with a unique message to differentiate the two.
Let us now try to register the completion function to the run.sh
in scripts
by cd'ing into the scripts directory and running complete -F _customcomplete ./run.sh
. Now I can call ./run.sh
and use tab completion to see the possibilities of one two three
. However, if I try and call it from a directory above as ./scripts/run.sh
, I get the default filesystem tab completion suggestions. Additionally, if I cd
into other
, the custom tab completion works on that script as well.
Changing the complete
command to link to any sort of global path only allows the custom tab completion when using that exact same global path as the invocation string. What I want to happen is to be able to call the script by any relative path and have the custom tab completion work as expected.