Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created February 7, 2023 20:13
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 rawiriblundell/4a978b7e715fd4444254780998fb648a to your computer and use it in GitHub Desktop.
Save rawiriblundell/4a978b7e715fd4444254780998fb648a to your computer and use it in GitHub Desktop.
Standard scp autocompletion was breaking on my ssh config structure. This fixes that by focusing on known_hosts only. If it's in the configs, then it should be in known_hosts, right?
__complete_ssh_host() {
local ssh_known_file ssh_known_list partial_word
ssh_known_file="${HOME}/.ssh/known_hosts"
[[ -r "${ssh_known_file}" ]] || return 1
ssh_known_list=$(awk '{print $1}' "${HOME}"/.ssh/known_hosts | tr ',' '\n' | sort | uniq)
partial_word="${COMP_WORDS[COMP_CWORD]}";
mapfile -t COMPREPLY < <(compgen -W "${ssh_known_list}" -- "${partial_word}")
return 0
}
complete -F __complete_ssh_host ssh
complete -f -F __complete_ssh_host scp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment