Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash AutoComplete from known_hosts
# add to ~/.bash_profile, and close/reopen a shell. Will autocomplete any hosts found in known_hosts.
complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh

This comment has been minimized.

Copy link

Raidenthequick commented Oct 7, 2014

This is cool, but would you know how to get it to complete only after the "user@" part? It treats the "user@host" as a whole string so completion fails after the @. Thanks.


This comment has been minimized.

Copy link
Owner Author

peelman commented Nov 16, 2015

@walentys so didn't even know you commented here...was just looking around at some old gists and saw this...

due to some issues somewhere in my past, I trained myself to use ssh's -l flag to set the login and don't use the user@host syntax except in rare cases. Don't remember the specifics of why, it just became a thing.


This comment has been minimized.

Copy link

dtwilliamson commented Jun 27, 2019

complete -W "$(awk -F , '/\[/ {hosts[$1]} END {for (host in hosts) {print host}' "$HOME/.ssh/known hosts")"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.