0

I know there are a lot of tutorials on bash completion, but I just can't figure this out.

All I want is this. If I type myscript[tab][tab], then "myscript list-commands" is run. It will output a space delimited list of available commands (but I can output it however is appropriate). That output list is used for tab completion.

What do I put it my .bashrc to make that happen?

tscheingeld
  • 789
  • 7
  • 24
  • The [`complete`](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html) builtin can specify the type of autocompletion for a given function. For more info see `help complete` or `man bash` – builder-7000 Jun 02 '19 at 19:42
  • That's close to what I want. When I asked the original question, I didn't realize that completion works not just for the first option but for all subsequent options. Here's what I have so far which isn't quite what I want: complete -C "myscript list-commands" myscript That means that if I type in a command like this: myscript [tab][tab] I get a list of commands. Then if I complete one of the commands, then hit [tab][tab] again, I get the list of commands again. What I want is that second time I get a list of *.sql files in the current directory. How do I do that? – tscheingeld Jun 02 '19 at 21:29

1 Answers1

1

The easiest way is using a list of words/commands that your script supports:

Put the following into your .bashrc to have your script myscript support the commands add, list, delete:

complete -W "add list delete" myscript

This will leads to

> myscript [tab][tab]
add list delete

Hope this help. For further, more dynamic options than a simple wordlist have a look at the manpage of the complete command: https://ss64.com/osx/complete.html

Florian Schlag
  • 639
  • 4
  • 16