4

I'm working on bash auto-completion for a project I'm the maintainer of. You can find the script here. I've cobbled this together with some hacking on my own, and with the help of some contributors who understand that completion APIs better than I do.

What we have works great -- with one exception. We can manage a completion like like this

//type
pestle.phar som[TAB]

//completes to 
pestle.phar some-command-name 

However, once we're here we lose file path/name completion that's a part of the stock bash shell. That is, working off the previous example, if a user types

//type
pestle.phar some-command-name /va[TAB]

we'd like it to complete to

//completes to the following, because var exists
pestle.phar some-command-name /var

Is there a way to just tell the complete command something like

Hey, in addition to everything we're telling you to do with our custom bash function, also keep your normal file path completion

If not, is there there some known science/boilerplate to reimplementing the file path completion in your own custom base completion functions?

Some other answers and the docs seem to indicate that the -o filenames or -o bashdefault options should take care of this -- but it doesn't seem to be working on OS X 10.11. I'm not sure if I misunderstand -o, or if the code in my completion files somehow overrides the -o behavior, or if OS X is doing it's I'm only a mostly well behaved unix thing.

Also -- if it's not obvious -- this is my first deep bash completion rodeo. If I've said something seemingly dumb/naive above please let me know. I may be looking for a fish right now, but I'd like to learn to fish in the bash completion river myself.

2 Answers 2

5

I think -o default (without -o filenames) should work for you. According to the manual:

  • bashdefault
    Perform the rest of the default bash completions if the compspec generates no matches.
  • default
    Use readline's default filename completion if the compspec generates no matches.
  • filenames
    Tell readline that the compspec generates filenames, so it can perform any filename-specific processing (like adding a slash to directory names, quoting special characters, or suppressing trailing spaces). Intended to be used with shell functions.

(Also see 'complete -d -o default cd' issue for the difference between -o default and -o bashdefault.)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Thank you @whjm, that did the trick and taught me that bash autocomplete and readline auto complete are two different things.
0

Bash completion provides the function _filedir which fills up COMPREPLY (i.e. it doesn't return anything, it just puts things in COMPREPLY).

If complete -o filenames -F _pestleAutocomplet pestle is not accurate enough (for example, -f requires a fruit name and you want pestle -f app to complete to -f apple and you don't want filenames to be considered here, you can have more fine grained control by triggering filename completion yourself.

For example

_pestleAutocomplete(){
    local cur prev words cword
    _init_completion || return

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${pestle_options_list}" -- "${cur}") )
    else
        case ${prev} in
            -f) COMPREPLY=( $(compgen -W "${peslte_fruit_names}" -- "${cur}") ) ;;
            -d) _filedir -d ;;
            *) _filedir ;;
        esac
    fi
}

this way, you don't get filename completion for arguments to -f, you can also get directory-only completion for -d by calling _filedir -d and for every other case you call _filedir.

Using complete -o default -F _pestleAutocomplet pestle would also work but still, if you do pestle -f cucumb, and you have a file named cucumber, then the compspec will generate no matches and filename completion will happen giving you pestle -f cucumber which is invalid because cucumber is not a valid pestle fruit name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.