Skip to content

Instantly share code, notes, and snippets.

@muniu
Forked from magnetikonline/README.md
Created June 12, 2018 06:17
Show Gist options
  • Save muniu/a0ae25aad978cb14e681d57fa16e8f60 to your computer and use it in GitHub Desktop.
Save muniu/a0ae25aad978cb14e681d57fa16e8f60 to your computer and use it in GitHub Desktop.
OS X - SSH client autocomplete for hosts.

OS X SSH client autocomplete for hosts

Snippet for ~/.bash_profile, adding hostname autocomplete to ssh.

Extracts host hints from both ~/.ssh/config and /etc/hosts.

function _completeSSHHosts {

	COMPREPLY=()
	local currentWord=${COMP_WORDS[COMP_CWORD]}

	local completeHosts=$(
		cat "$HOME/.ssh/config" | \
		grep --extended-regexp --regexp "^Host +[^* ]+? *$" | \
		tr -s " " | cut -d " " -f 2;
		cat /etc/hosts | \
		grep --extended-regexp --regexp "^[0-9]{3}\." | \
		awk "{print \$2}"
	)

	COMPREPLY=($(compgen -W "$completeHosts" -- "$currentWord"))
	return 0
}

complete -F _completeSSHHosts ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment