Skip to content

Instantly share code, notes, and snippets.

@srpatel
Last active November 25, 2021 13:52
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 srpatel/4ab0ffb532569383f795 to your computer and use it in GitHub Desktop.
Save srpatel/4ab0ffb532569383f795 to your computer and use it in GitHub Desktop.
# Source this file in your .profile/.bashrc in order to get tab autocomplete
# on ssh hosts as listed in your ~/.ssh/config file.
_ssh_autocomplete() {
local SSH_CONFIG_FILE="$HOME/.ssh/config"
local cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$COMP_CWORD" -eq "1" && -f $SSH_CONFIG_FILE ]]; then
local hosts=`cat $SSH_CONFIG_FILE | grep ^Host | awk '{ $1=""; print $0 }' | tr '\n' ' '`
COMPREPLY=( $(compgen -W "$hosts" -- $cur) )
else
COMPREPLY=( $(compgen -o nospace -f ${cur}) ); #normal
fi
}
complete -F _ssh_autocomplete ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment