15

Say I have a command named "foo" that takes one argument (either "encrypt" or "decrypt") and then a list of files. I want to write a bash completion script that helps with the first argument and then enables standard file completion for the others. The closest I've been able to come is this:

complete -F _foo foo

function _foo()
{
    local cmds cur
    if [ $COMP_CWORD -eq 1 ]
    then
        cur="${COMP_WORDS[1]}"
        cmds=("encrypt decrypt")
        COMPREPLY=($(compgen -W "${cmds}" -- ${cur}) )
    else
        COMPREPLY=($(compgen -f ${COMP_WORDS[${COMP_CWORD}]} ) )
    fi
}

This does the first argument correctly and will chose a filename from the current directory for the subsequent ones. But say the current directory contains a subdirectory bar that contains a file baz.txt. After typing ba-TAB-TAB, completion results in "bar " (space after the "r") and is ready to choose the next argument. What I want is the standard bash completion, where the result is "bar/" (no space after the slash), ready to choose a file in the subdirectory. Is there any way to get that?

Mike Schilling
  • 3,203
  • 1
  • 14
  • 8

2 Answers2

25

I know this is a little late, but I have found a solution here: https://stackoverflow.com/a/19062943/108105

Basically, you use complete -o bashdefault -o default, and when you want to revert to the default bash completion you set COMPREPLY=(). Here's an example:

complete -o bashdefault -o default -F _foo foo
_foo() {
    local cur=${COMP_WORDS[COMP_CWORD]}
    if (( $COMP_CWORD == 1 )); then
        COMPREPLY=( $(compgen -W 'encrypt decrypt' -- "$cur") )
    else
        COMPREPLY=()
    fi
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
  • +1 Very neat. It reverts to readline defaults not bash though, not that it affects the OP, but you loose bashdefault completions ($, ~ , @). I think `compopt -o bashdefault` (bash-4.0) can help there. – mr.spuratic May 13 '14 at 17:12
14

The bash documentation can be a little enigmatic. The simplest solution is to alter the completion function binding:

complete -o filenames -F _foo foo

This means the function returns filenames (including directories), and special handling of the results is enabled.

(IMHO the documentation doesn't make it clear that this effectively post-processes COMPREPLY[] as set by your completion function; and that some of the -o options, that one included, when applied to compgen appear to have no effect.)

You can get closer to normal bash behaviour by using:

 complete -o filenames -o bashdefault -F _foo foo

that gets you "~" completion back.

There are two problems with the above however:

  • if you have a directory named "encrypt" or "decrypt" then the expansion of your keywords will grow a trailing "/"
  • $VARIABLE expansion won't work, $ will become \$ to better match a filename with a $. Similarly @host expansion won't work.

The only way that I have found to deal with this is to process the compgen output, and not rely on the "filenames" post-processing:

_foo()
{
    local cmds cur ff
    if (($COMP_CWORD == 1))
    then
        cur="${COMP_WORDS[1]}"
        cmds="encrypt decrypt"
        COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
        COMPREPLY=( "${COMPREPLY[@]/%/ }" )   # add trailing space to each
    else
        # get all matching files and directories
        COMPREPLY=($(compgen -f  -- "${COMP_WORDS[$COMP_CWORD]}"))

        for ((ff=0; ff<${#COMPREPLY[@]}; ff++)); do
            [[ -d ${COMPREPLY[$ff]} ]] && COMPREPLY[$ff]+='/'
            [[ -f ${COMPREPLY[$ff]} ]] && COMPREPLY[$ff]+=' '
        done
    fi
}

complete -o bashdefault -o default -o nospace -F _foo foo

(I also removed the superfluous array for cmd in the above, and made compgen more robust and handle spaces or leading dashes in filenames.)

The downsides now are that when you get the intermediate completion list (i.e. when you hit tab twice to show multiple matches) you won't see a trailing / on directories, and since nospace is enabled the ~ $ @ expansions won't grow a space to cause them to be accepted.

In short, I do not believe you can trivially mix-and-match your own completion and the full bash completion behaviour.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
mr.spuratic
  • 9,767
  • 3
  • 34
  • 24