Skip to content

Instantly share code, notes, and snippets.

@fieg
Created April 11, 2012 07:57
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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
@nicbou
Copy link

nicbou commented Jun 17, 2014

I had to cut the cat ~/.ssh/config part for it to work on my end since the file didn't exist. It works flawlessly now. Thanks!

@crookedstorm
Copy link

Saved me a bit of fussing with bash. Thanks!

@mattdeboard
Copy link

Nice. Thanks.

@rotarur
Copy link

rotarur commented Nov 20, 2015

Nice workaround. Thnks

@warfares
Copy link

great !! thanks :-)

@Meekohi
Copy link

Meekohi commented Oct 18, 2016

This was great! Thanks!

@javenschuetz
Copy link

+1

@vladislav-vorobev-epam
Copy link

I've got strange '^M' suffixes after each hostname in autocomplete. Like 'my-demo-stand-1^M' instead of 'my-demo-stand-1'
Can anybody help?

@somedrunkbum
Copy link

@vladislav-vorobev-epam did you manage to solve this issue?

@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