Skip to content

Instantly share code, notes, and snippets.

@peterkeller
Forked from khun84/README.md
Last active March 22, 2019 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterkeller/d3c9ec33b58225272382d4f343300c55 to your computer and use it in GitHub Desktop.
Save peterkeller/d3c9ec33b58225272382d4f343300c55 to your computer and use it in GitHub Desktop.
Mac OS X - SSH client host autocomplete

Mac 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 "^Host +([^* ]+ +)*[^* ]+ *$" | \
			tr -s " " | \
			sed -E "s/^Host +//"
	)

	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