Skip to content

Instantly share code, notes, and snippets.

@romantomjak
Created January 15, 2018 20:34
Show Gist options
  • Save romantomjak/6376d8b6b3b177a6cdae50ec994d22d0 to your computer and use it in GitHub Desktop.
Save romantomjak/6376d8b6b3b177a6cdae50ec994d22d0 to your computer and use it in GitHub Desktop.
Bash autocomplete for hosts defined in SSH config

Open or create ~/.bash_profile and add the following lines:

# ssh autocomplete
function _ssh_autocomplete() {
  COMPREPLY=()
  CURRENT_WORD="${COMP_WORDS[COMP_CWORD]}"
  if [[ ${CURRENT_WORD} == -* || ${COMP_CWORD} -eq 1 ]] ; then
    WORD_LIST=$(sed -nE "s/Host\ ([a-zA-Z0-9].+)/\1/p" ~/.ssh/config | tr '\n' ' ' )
    COMPREPLY=( $(compgen -W "$WORD_LIST" -- ${CURRENT_WORD}) )
  fi
}
complete -F _ssh_autocomplete ssh

This will allow you to list hosts defined in ~/.ssh/config by typing ssh <tab> in the Terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment