[Bash-completion-devel] Get stuck

Raph gibboris at gmail.com
Mon May 23 20:51:09 UTC 2011

On Mon, May 23, 2011 at 03:45:39AM +0400, Igor Murzov wrote:
> Hey! I get stuck trying to implement completion script for strings containing 
> two parts for completion delimited by '%' sign.  For example, `cmd opt%arg`. 
> The problem is that, if i hit tab key, when cursor is behind delimeter, bash 
> eats first part of string leaving `cmd arg`. I just can't understand why, for 
> example `cmd -opt=arg` works just fine, and `cmd opt%arg` is not. What is the 
> difference?
> I also wrote the simple script to demonstrate the issue:
> ---------------------------------------------------------
> _zz()
> {
>     COMPREPLY=()
>     local cur="${COMP_WORDS[COMP_CWORD]}"
>     if [[ "$cur" == ?*%* ]]; then
>         cur="${cur#*%}"
>         COMPREPLY=( $( compgen -W 'abc bbb cde' -- "$cur" ) )
>         return 0
>     fi
>     compopt -o nospace
>     COMPREPLY=( $(compgen -W 'xxx yyy zzz' -- "$cur") )
> } && complete -F _zz zz
> ---------------------------------------------------------
> '_init_completion -n %' does not solve the issue either.
> -- Igor
> _______________________________________________
> Bash-completion-devel mailing list
> Bash-completion-devel at lists.alioth.debian.org
> http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/bash-completion-devel

At least, the following is correct, and works:

    local cur
    _get_comp_words_by_ref cur

    if [[ "$cur" == ?*%* ]]; then
        COMPREPLY=( $( compgen -W "${pref}abc ${pref}bbb ${pref}cde" -- "$cur" ) )
        return 0

- I could not get compgen -P "${pref}" to work (sadly)
- you should know that what the word finally written by the completion
process at the end of the command-line is always exactly the same string
than the one which was previously written among the suggestions below the
- if you need/want to deal with "suffixes" only, you'll only want suffixes to
be suggested, thus you'll only need suffixes in the compgen string (and
in -- "${cur}")
So I *guess* that you'll have to find how to do the opposite of the -n
flag of _get_comp_words_by_ref(). (the -n flag excludes a character from

good luck

PS: let me know if you find a proper solution, I had a similar problem
with completions starting by 'http://'.


More information about the Bash-completion-devel mailing list