0

I'm creating a new command and I need a completion function so that when I press tab a couple of times it gives me some alternatives to choose from. The list of alternatives should be the branches available in a git repository.

I've used complete -W $(git branch | grep -v '^*' | xagrs) my_command but that creates a static list once .bashrc is "compiled" and it's not dynamic per repository.

Also I've found complete -c but that doesn't seem to work.

Any ideas?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Abdallah
  • 402
  • 4
  • 14

1 Answers1

1

complete has -F argument that let's you generate complete from inside any function. Inside that function run your command git branch | grep -v '^*' and fill out COMPREPLY appropriately. There are many guides on how to write bash completion functions on the net available - research them and write your own functions for your needs.

Notes: -W $(...) - the unquoted expansion undergoes word splitting, so you are effectively running complete with a single completion argument. Be sure to research which, how to and when to use quotes in shell.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111