2

I'm trying to add auto-complete function to mytool command with following _mytool complete function:

_mytool()
{
  local cur
  _get_comp_words_by_ref -n : cur

  # my implementation here
  COMPREPLY=()   # Array variable storing the possible completions.
  cur=${COMP_WORDS[COMP_CWORD]}

  if [[ $cur = -* ]]; then
    COMPREPLY=( $(compgen -W '-a:first -a:second' -- $cur) )
  fi

  __ltrim_colon_completions "$cur"
}
complete -F _mytool mytool

Because there is ":" in my COMPREPLY, I try to make it work by using _get_comp_words_by_ref and __ltrim_colon_completions function, which is learnt from: here

But it still not work when type:

$mytool -a:[TAB]

there is no auto-complete at all. I was expecting bash will print the following completion for me:

-a:first -a:second

my bash version:

4.3.46(1)-release

What am I missing? Thanks!

zwy
  • 139
  • 1
  • 7
  • I'm coming here with the same issue - it seems that the _get_comp_words_by_ref and __ltrim_colon_completions functions still have no documentation anywhere that I can find - so it's unclear how you're supposed to use them. I'm commenting in the hopes that your question becomes more visible. – Lou Dec 10 '20 at 10:39
  • @Lou just stumbled on this accidentally. `__get_comp_words_by_ref` seems to be defined in the `/usr/share/bash-completion/bash-completion` script (line 36) loaded via your `.bashrc`. The function is preceded by a nice header explaining usage and available flags. – Tasos Papastylianou Jun 10 '22 at 18:08

0 Answers0