Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active May 21, 2023 08:15
Show Gist options
  • Save magnetikonline/bcd4186e14ed02145390 to your computer and use it in GitHub Desktop.
Save magnetikonline/bcd4186e14ed02145390 to your computer and use it in GitHub Desktop.
macOS SSH client host autocomplete.

macOS SSH client host autocomplete

Snippet for ~/.bash_profile, adding hostname autocomplete to ssh and scp.

Extracts host hints from ~/.ssh/config.

function __completeSSHHosts {
  COMPREPLY=()
  local currentWord=${COMP_WORDS[COMP_CWORD]}
  local completeHosts=$(
    cat "$HOME/.ssh/config" | \
      grep --extended-regexp "^Host +([^* ]+ +)*[^* ]+ *$" | \
      tr -s " " | \
      sed -E "s/^Host +//"
  )

  COMPREPLY=($(compgen -W "$completeHosts" -- "$currentWord"))
}

complete -F __completeSSHHosts scp ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment