Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

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

1 Answer

up vote 34 down vote accepted

bash-completion adds completion for:

  • Command names after sudo and which
  • Macports and Homebrew package names (optional)
  • Hostnames in known_hosts for commands like ssh
  • Folders on CDPATH

And so on. You can print a list of completion commands with complete -p and see the source of a function with declare -f.

Installing bash-completion with Homebrew

$ brew install bash-completion
==> Downloading http://bash-completion.alioth.debian.org/files/bash-completion-1.3.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3
==> make install
==> Caveats
Add the following lines to your ~/.bash_profile file:
  if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
  fi

To install Homebrew's own completion script:
  ln -s "/usr/local/Library/Contributions/brew_bash_completion.sh" "/usr/local/etc/bash_completion.d"
==> Summary
/usr/local/Cellar/bash-completion/1.3: 187 files, 1.0M, built in 29 seconds

Homebrew currently installs an older version of bash-completion (1.3) that still works with Bash 3.x.

Installing bash-completion with MacPorts

$ sudo port install bash-completion

...

To use bash_completion, add the following lines at the end of your .bash_profile:
  if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
      . /opt/local/etc/profile.d/bash_completion.sh
  fi
The port bash-completion >=2.0 requires bash >=4.1; please make sure
you are using /opt/local/bin/bash by changing the preferences of your
terminal accordingly. If your version of bash is too old, the script
above will not modify your shell environment and no extended completion
will be available.

See trac.macports.org/wiki/howto/bash-completion for instructions on how to enable completion for port names.

share|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
    
Thanks for the tip about autocompletion in homebrew. That alone earned this answer an up-vote. –  Paul Wagland Jul 10 '11 at 23:02
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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