50

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!

slhck
  • 223,558
  • 70
  • 607
  • 592
Osama Gamal
  • 1,137
  • 2
  • 9
  • 9

1 Answers1

82

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 Homebrew or MacPorts and Bash

First, you have to install Homebrew or MacPorts according to the instructions. Note: Do not install both, as they conflict.

Then, install a newer version of Bash. The built-in Bash in OS X is a little old, and you'll get more completion options with Bash > 4.1, which you can get through

brew install bash

or

sudo port install bash

depending on whether you use Homebrew or MacPorts.

Installing bash-completion with Homebrew

To install bash-completion, you have to:

brew install bash-completion

And add the following to your ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

Homebrew currently installs an older version of bash-completion (1.3) that still works with Bash 3.x, but still, using Bash 4.x is recommended.

Installing bash-completion with MacPorts

With MacPorts:

sudo port install bash-completion

Then, add to your ~/.bash_profile:

if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
  . /opt/local/etc/profile.d/bash_completion.sh
fi

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

slhck
  • 223,558
  • 70
  • 607
  • 592
  • MacPorts' `bash-completion` package provides support for completion of package names as additional arguments after `port `. See [here](http://trac.macports.org/browser/trunk/dports/sysutils/bash-completion/files/port), lines 48-58. This behavior is hinted at in [the MacPorts wiki](http://trac.macports.org/wiki/howto/bash-completion), which states: "*This is not just for files and directories, but also e.g. for the commands of `port`. So you type `port ` 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
  • 1
    Thanks for the tip about autocompletion in homebrew. That alone earned this answer an up-vote. – Paul Wagland Jul 10 '11 at 23:02
  • Excellent, this enabled `make` autocomplete, just what I wanted. – Matthew Turner Nov 26 '13 at 17:46
  • if you happen to have both Brew and MacPorts make sure to add the macports one before Brew as it checks for $BASH_COMPLETION_COMPAT_DIR and exits if populated. – Marlon Dec 22 '15 at 23:32
  • _excellent_ instructions here! – WestCoastProjects Jun 09 '19 at 22:50
  • Thanks for the answer, @slhck! I still wanted auto-complete for Git and I found a good solution here https://www.macinstruct.com/tutorials/how-to-enable-git-tab-autocomplete-on-your-mac/ – SRG Oct 04 '21 at 09:42