Skip to content

Instantly share code, notes, and snippets.

@spacegaucho
Last active January 17, 2018 14:18
Show Gist options
  • Save spacegaucho/dffb3139f5219b093f6a2253fcfccb57 to your computer and use it in GitHub Desktop.
Save spacegaucho/dffb3139f5219b093f6a2253fcfccb57 to your computer and use it in GitHub Desktop.
Bash completion for ssh
# Store in /etc/bash_completion.d/ssh
# Create a ~/.ssh/hosts file that includes the hostnames you want to autocomplete
# Example: `Host hostname`
# Found this idea here https://unix.stackexchange.com/a/181603
_ssh()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' ~/.ssh/config ~/.ssh/hosts| grep -v '[?*]' | cut -d ' ' -f 2-)
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _ssh ssh
# You can add any aliases that do different things here too
# complete -F _ssh sshalias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment