0

You normally interact with bash completion by pressing the tab key in your terminal. I would like to interact with it within a script of mine. Essentially, I would like a function/command that answers the question "If I pressed tab with text xyz already typed and the cursor as position n, what would be the suggestions?"

Does this function exist? I found compgen but it's poorly documented and doesn't seem to do what I want.

Drew
  • 12,578
  • 11
  • 58
  • 98

1 Answers1

1

The compgen seems to be exactly what you'd like to use anyway. To display what would happen if you'd be typing com and hitting the TAB key, use:

compgen -c com

Or:

compgen -A command com

This will output all Bash builtins along with external binaries and scripts found from the PATH.

More information can be found from the documentation in case you'd like to, for example, prune out from the output of compgen all Bash builtins and display just the external binaries/scripts.

  • 1
    This doesn't seem to work. If I type `git sta` I get 3 suggestions: `stage`, `stash`, and `status`. But if I run `compgen -c 'git sta'` I get no suggestions. – Drew Oct 14 '16 at 17:54
  • Well, it isn't the Bash completion doing the completion for git subcommands, so it'll never work as you'd like it to work. However, compgen *does* produce for you what Bash would produce. –  Oct 16 '16 at 09:52
  • Well, that wasn't very clear of me. It's Bash, but it's not *vanilla* Bash doing the completion. You don't state the system you're running Bash in, but assuming it's a GNU/Linux distribution of a kind, you'll probably find `/etc/profile.d/bash_completion.sh` (or something named similarly) which either contains the magic itself or sources the magic, from say `/usr/share/bash-completion/bash_completion`. The paths mentioned in this comment are from Ubuntu 14.04 LTS; so, your distribution might use different filenames but the mechanism is still the same. –  Oct 17 '16 at 08:34
  • So it's rather system dependent then. I want to mimic bash's tab completion, but I don't really want to re-implement all of bash's logic for looking up and loading/sourcing/whatever these files. Is there a way I can do that? Maybe some flag or input I can give to `bash` itself, like `bash -c "git s\t"` or something? – Drew Oct 18 '16 at 06:09
  • Unfortunately not. If you want to mimic *vanilla* Bash, then your best bet is what's mentioned above ie. `compgen`. If you want to mimic what's going on in the particular system you're using -- where git subcommands get completed too -- you'd have to implement all the magic on top of compgen yourself. At this point it might be a good idea to settle with `compgen`, as anything more is not likely to justify the trouble of implementation. –  Oct 18 '16 at 06:56
  • I have exactly the same question as OP. But I'm not sure I understand the conclusion. @SamiLaine are you saying that it is *impossible* to find out what the completion of a given string (f.ex. "ssh webser") would be by using the `compgen` and `complete`? – Tomáš Pospíšek May 01 '20 at 07:42