38

I'm trying to create my ~/.ssh/config file for simplify my work life on the server connection.

In Mavericks all worked properly and with the tab I was be able to autocomplete the hostname with file like this:

Host test-host-name
    HostName 192.168.1.2
    User root

The same configuration on Yosemite have a different behavior. When I type ssh t on the shell the tab does not autocomplete the hostname.

Do someone know if is something changed in Yosemite and now I need to do something to make it work?

37

You can enable bash shell completion by installing bash-completion via brew:

brew install bash-completion

Then add the following to your ~/.bash_profile:

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

Source

(Edit: brew tap homebrew/completions is deprecated as of 2018.)

  • +1 :) When installing bash-completion-1.3.3.high_sierra, it tells me I should add [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion. Could you elaborate on which version (that or yours) should be preferred and why? – Jan Nash Jun 17 '18 at 2:41
15

I assume the shell of your Mac is bash.

You need to complete ssh command, for more detail you can read man complete

_complete_ssh_hosts ()
{
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
                        cut -f 1 -d ' ' | \
                        sed -e s/,.*//g | \
                        grep -v ^# | \
                        uniq | \
                        grep -v "\[" ;
                cat ~/.ssh/config | \
                        grep "^Host " | \
                        awk '{print $2}'
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh

Don't forget you need to source the file to get the new command to load in your terminal. or You can put this code in .bash_profile

  • Yep, the shell is bash. Why in Maveriks I didn't need to do this things? – RikyTres Jan 20 '15 at 13:12
  • No, I've tested just now. I can't do that in Mavericks by default. – nelson Jan 20 '15 at 13:19
  • comp_ssh_hosts=$(awk -F , 'FNR== NR && ! /^#|\[/ {field = $1} FNR != NR && /^Host / {field = $2} {hosts[field]} END {for (host in hosts) {print host}' "$HOME/.ssh/known hosts" "$HOME/.ssh/config") – Paused until further notice. Jun 27 at 18:10
2

From this article on MacWorld.

Copy/paste the following into ~/.bash_profile:

complete -o default -o nospace -W "$(/usr/bin/env ruby -ne 'puts $_.split(/[,\s]+/)[1..-1].reject{|host| host.match(/\*|\?/)} if $_.match(/^\s*Host\s+/);' < $HOME/.ssh/config)" scp sftp ssh

You'll then need to either restart your terminal or type source ~/.bash_profile (note that the latter will only work in your current tab).

0

FYI, I'm not sure how to respond directly to nelson's answer since I haven't built up enough reputation to comment on it yet, but on my machine running El Capitan, ^M was being appended to all of the host names from my config file. I had to either add \r to my $IFS variable or remove/substitute all instances of \r from the output of the second cat. In the end, I did the latter by adding tr '\r' '\n' to make that command chain look like this:

cat ~/.ssh/config | \
        grep "^Host " | \
        awk '{print $2}' | \
        tr '\r' '\n'
-1

You can add entries in /etc/hosts and use them in host name autocomplete:

"ssh @" + [TAB]

Reboot is required for system to reload this file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .