4

Let's say I have a bash shell function named magic. I want to define a tab completion function _magic which would allow magic to piggyback on the tab completion functions of any given command (if available). In other words, I want magic to be able to do something like this:

~ $ magic git ... <search for _git and use it if found>
~ $ magic cd ... <search for _cd and use it if found>
~ $ magic some-cmd ... <search for _some-cmd and use it if found>

I can't seem to find anything online that would help me achieve this using compgen, complete etc. Is this even possible? Thanks in advance.

smartrahat
  • 5,381
  • 6
  • 47
  • 68
Joohwan
  • 2,374
  • 1
  • 19
  • 30
  • 2
    Maybe there is something useful in this question: http://stackoverflow.com/questions/30537074/use-compgen-to-get-autocomplete-for-another-command-which-flag-to-use – rici Feb 12 '16 at 04:03
  • See if running this in bash helps: `complete -F _command magic` – anishsane Feb 12 '16 at 06:55

1 Answers1

3

There are many such (pseudo?) commands, that need this. e.g. time, nice, strace etc.
They all use the same command completion: _command. So, don't re-invent the wheel. :)

Try running this in your bash terminal, if this work:

complete -F _command magic
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Hello anishsane, I am aware of complete -F. What I want is to dynamically switch the _command depend on the first argument to my "magic" function. Are you saying I should literally call "complete -F _command magic" in my _magic function? – Joohwan Feb 12 '16 at 15:52
  • No, there should not be any `_magic` function. Just try the command I gave. – anishsane Feb 15 '16 at 04:56