How do I get fink and macports to autocomplete program names when typing commands, as in Ubuntu's apt-get?

For example when I type

sudo port install ca

and press tab, I want the terminal to autocomplete or show me the available commands.

OS X does not even auto-complete commands after sudo!

link|improve this question
Upvoting the question, since it generated exactly the answer that I wanted. – Paul Wagland Jul 10 '11 at 23:03
feedback

1 Answer

up vote 11 down vote accepted

A method would be to add the auto completion package using MacPorts or Homebrew. I would advise you to switch to Homebrew anyway if you're still using MacPorts. Just a personal preference though.

Install the bash-completion package

Anyway, install the bash-completion package:

sudo port install bash-completion
brew install bash-completion

This also works with Fink too, I suppose.

You should then edit your ~/.bash_profile and add the following if you used MacPorts:

if [ -f /opt/local/etc/bash_completion ]; then
 /opt/local/etc/bash_completion
fi

If you instead used Homebrew, add the following:

if [ -f `brew --prefix`/etc/bash-completion ]; then
 . `brew --prefix`/etc/bash-completion
fi

Restart the Terminal or open a new shell. Now sudo should autocomplete for you, and a couple of other things as well.

I'm afraid though that MacPorts by itself won't autocomplete, and Bash just doesn't know which packages exist. Luckily, there's remedy for that.


Install autocompletion for MacPorts

According to the MacPorts Wiki you can enable autocompletion for port <command>. It also completes package names. Furthermore, it should enable autocompletion for all other installed ports.

Add the following line to /opt/local/etc/macports/variants.conf:

+bash_completion

Then restart the Terminal or open a new shell. Thanks to Daniel Beck and Kevin Reid for pointing those things out.

Install autocompletion for Homebrew

You can install Homebrew's own autocompletion script by executing:

ln -s "/usr/local/Library/Contributions/brew_bash_completion.sh" "/usr/local/etc/bash_completion.d"

and then restarting the Terminal. Now Homebrew will autocomplete its own commands and also its packages. Problem solved.

link|improve this answer
First time to know about Homebrew .. Thanks :D – Osama Gamal May 25 '11 at 17:03
MacPorts' bash-completion package provides support for completion of package names as additional arguments after port <verb>. See here, lines 48-58. This behavior is hinted at in the MacPorts wiki, which states: "This is not just for files and directories, but also e.g. for the commands of port. So you type port <Tab> and get a list of all possible commands.", although it only explicitly mentions completion of the verbs (which is much simpler). – Daniel Beck May 25 '11 at 17:40
The package names are autocompleted, the wiki just doesn't mention that. See the linked script code. – Daniel Beck May 25 '11 at 18:24
The variants.conf is not for completion for MacPorts itself; variants.conf defines variants to automatically set for all ports, as is explained on the wiki page you linked. Adding that line causes bash completion support to be enabled when possible for ports you install. – Kevin Reid May 25 '11 at 23:26
@Kevin Thank you very much, I included it. – slhck May 26 '11 at 5:40
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.