Here is a related question and another suggested solution:
How to reset COMP_WORDBREAKS without effecting other completion script?
As stated before, the simplest solution is to alter COMP_WORDBREAKS
. However, modifying COMP_WORDBREAKS
in your completion script is not safe (as it is a global variable and it has the side effect of affecting the behavior of other completion scripts - for example scp).
Therefore, bash completion offers some helper methods which you can use to achieve your goal in a better and more safer way.
Two helper methods were added in Bash completion 1.2 for this:
_get_comp_words_by_ref
with the -n EXCLUDE
option
- gets the word-to-complete without considering the characters in EXCLUDE as word breaks
__ltrim_colon_completions
So, here is a basic example of how to a handle a colon (:) in completion words:
_mytool()
{
local cur
_get_comp_words_by_ref -n : cur
# my implementation here
__ltrim_colon_completions "$cur"
}
complete -F _mytool mytool
Using the helper methods also simplifies the completion script and ensures that you get the same behavior on any environment (bash-3 or bash-4).
You can also take a look at man
or perl
completion scripts in /etc/bash_completion.d
to see how they use the above helper methods to solve this problem.