7

I need the directory names in /mailman/lists to tab-complete as an argument (without any slashes) to the 'list_members' command regardless of where I am in the filesystem.

In tcsh (on our old server), I wrote one line to do what I needed:

complete list_members 'p,*,F:/mailman/lists,,' #tcsh

bash's implementation seems to be more complex. How can I implement this same behavior using bash?

The tab-completion needs to be context-sensitive so that whatever part of the argument I've already typed narrows down the search, just like standard bash completion. Example:

list_members sys<TAB> #only shows names beginning with sys

I've tried a few solutions after looking at examples, but they don't work properly.

One failing example:

function _listlists()
{
    COMPREPLY=( $( compgen -C 'ls /mailman/lists' ) )
}

complete -F _listlists list_members

Which yields the error:

-bash: compgen: warning: -C option may not work as you expect

Another failing example:

function _listlists()
{
    cd /mailman/lists
    COMPREPLY=( $( compgen -d ) )
    cd -
}

complete -F _listlists list_members

This shows all results from that dir piped to 'more', but it doesn't respond to the partial argument I've already typed.

Thanks.

zenatom
  • 121

2 Answers2

5

My colleague came up with this solution right after I posted the question. It works well:

function _listlists()
{
    local cur
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($( compgen -W "$(ls -d /mailman/lists/*/|cut -d "/" -f 4)" -- $cur ) )
}

complete -F _listlists list_members
zenatom
  • 121
3

I tested a solution with the HOME-folders (no mailman on my host). Is this what you are looking for?

function _homes() {
    local homes=("/home/$2"*)
    [[ -e ${homes[0]} ]] && COMPREPLY=( "${homes[@]##*/}" )
}
complete -F _homes homes
alias homes='echo you selected:'
return42
  • 131