Skip to content

Instantly share code, notes, and snippets.

@fieg
Created April 11, 2012 07:57
Show Gist options
  • Save fieg/2357723 to your computer and use it in GitHub Desktop.
Save fieg/2357723 to your computer and use it in GitHub Desktop.
.bash_profile for OSX including autocomplete for ssh hosts, default variables and some aliases
export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/sbin:$PATH
export EDITOR=vim
export APPLICATION_ENV="development"
export CLICOLOR=1
export LSCOLORS=dxfxcxdxbxegedabagacad
alias composer="php /usr/local/bin/composer.phar"
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
cat ~/.ssh/config | \
grep "^Host " | \
awk '{print $2}'
`
COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
return 0
}
complete -F _complete_ssh_hosts ssh
@ronappleton
Copy link

ronappleton commented Mar 22, 2018

My /.bash_profile looks as follows:

export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/sbin:$PATH
export EDITOR=vim
export APPLICATION_ENV="development"
export CLICOLOR=1
export LSCOLORS=dxfxcxdxbxegedabagacad

alias composer="php /usr/local/bin/composer.phar"

ulimit -n 4096

_complete_ssh_hosts ()
{
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
                        cut -f 1 -d ' ' | \
                        sed -e s/,.*//g | \
                        grep -v ^# | \
                        uniq | \
                        grep -v "\[" ;
                cat ~/.ssh/config | \
                        grep "^Host " | \
                        awk '{print $2}'
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh

And it does not work for me??

I have entries in both config and known_hosts

@difki
Copy link

difki commented Oct 13, 2018

Changed cat ~/.ssh/config --> ~/.ssh/known_host as I don't have ~/.ssh/config file.
Now it works perfectly.

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