Skip to content

Instantly share code, notes, and snippets.

@palawer
Created June 18, 2019 10:48
Show Gist options
  • Save palawer/8463c0d0f0017870e2b631bf7c8e78bb to your computer and use it in GitHub Desktop.
Save palawer/8463c0d0f0017870e2b631bf7c8e78bb to your computer and use it in GitHub Desktop.
Autocomplete SSH 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