Search code examples
bashmacosbash-completion

Custom Bash Autocomplete with File Path Completion


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.


Solution

  • 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.)