SSH Autocompletion – Disable using /etc/hosts – brew

I use the bash-completion keg from homebrew to get better bash-completion on Mac OS X. You can install it with

brew install bash-completion

You can also get some information about it using:

brew info bash-completion

It will tell you something like:

bash-completion: stable 1.3
/usr/local/Cellar/bash-completion/1.3 (187 files, 1.1M) *
==> Caveats
Add the following lines to your ~/.bash_profile:
  if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion

Homebrew's own bash completion script has been installed to

Bash completion has been installed to:

To find out where the file that does all the work is stored, run:

echo $(brew --prefix)/etc/bash_completion

It will probably tell you something like:


That file has a lot of comments in it, one of the gems being:


# Helper function for completing _known_hosts.
# This function performs host completion based on ssh's config and known_hosts
# files, as well as hostnames reported by avahi-browse if
# COMP_KNOWN_HOSTS_WITH_AVAHI is set to a non-empty value.  Also hosts from
# HOSTFILE (compgen -A hostname) are added, unless
# COMP_KNOWN_HOSTS_WITH_HOSTFILE is set to an empty value.
# Usage: _known_hosts_real [OPTIONS] CWORD
# Options:  -a             Use aliases
#           -c             Use `:' suffix
#           -F configfile  Use `configfile' for configuration settings
#           -p PREFIX      Use PREFIX
# Return: Completions, starting with CWORD, are added to COMPREPLY[]

The short story is that if you set up something in your ~/.bash_profile file like this:

export COMP_KNOWN_HOSTS_WITH_HOSTFILE="" #don't use /etc/hosts for ssh autocomplete

Then your ssh autocomplete will only be working from the ssh configuration file and not your /etc/hosts file – which if you have a lot of entries could be frustrating.

Related Posts:

  • No Related Posts
This entry was posted in Tech Tips and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *