mathiasbynens / dotfiles

:wrench: .files, including ~/.macos — sensible hacker defaults for macOS
https://mths.be/dotfiles
MIT License
30.27k stars 8.74k forks source link

Use .ssh/known_host for tab completion on ssh, scp, sftp #298

Open marcobalk opened 10 years ago

marcobalk commented 10 years ago

Why not use ~/.ssh/knownhosts for tab completion on ssh, scp, sftp. This way you have easier access to other hosts.

I use the following command:

HOSTLIST=$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | cut -f 1 -d ':' | cut -f 1 -d ']' | cut -d '[' -f2`;)
complete -W "${HOSTLIST}" ssh sftp scp
greygore commented 10 years ago

This worked well for me, although I cleaned it up to match the existing tab completion:

[ -e "${HOME}/.ssh/known_hosts" ] && complete -o "default" -o "nospace" -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | cut -f 1 -d ':' | cut -f 1 -d ']' | cut -d '[' -f2`;)" scp sftp ssh;
marcobalk commented 10 years ago

Thanks! Nice one. Now using it in my fork.