Skip to content

Instantly share code, notes, and snippets.

@coderatchet
Last active August 17, 2016 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderatchet/21709ec7ce9cb9b73429eac46430dd35 to your computer and use it in GitHub Desktop.
Save coderatchet/21709ec7ce9cb9b73429eac46430dd35 to your computer and use it in GitHub Desktop.
bash completion for virtual environments (virtualenv) in mac os x
export VENVS=$HOME/venvs # or whatever your virtualenvs folder is.
# first install brew_completion by calling `brew install brew-completion`
# will add bash_completion in the future. for now this is a place holder for when it will be needed.
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
# if you're placing the _act function and its alias in their corresponding files
if [ -f $HOME/.bash_functions ]; then
. $HOME/.bash_functions
fi
if [ -f $HOME/.bash_aliases ]; then
. $HOME/.bash_aliases
fi
###########################################################################
# following can also go in .bash_functions #
###########################################################################
_act() {
if [ -z ${1+x} ]; then
echo "usage: act <virtual environment in ${VENVS}>"
elif [ ! -d "${VENVS}/$1" ]; then
echo "${VENVS}/$1 is not a directory"
else
. "$VENVS/$1/bin/activate"
fi
}
###########################################################################
# following can also go in .bash_aliases #
###########################################################################
alias act=_act
###########################################################################
# following goes into $(brew --prefix)/etc/bash_completions.d/act #
###########################################################################
_act_completion()
{
local cword="${COMP_WORDS[COMP_CWORD]}"
local words=$(find ${VENVS:-"~/venvs"} -type d -maxdepth 1 -mindepth 1 -exec basename {} ';' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' -e 's/\///')
COMPREPLY=$(compgen -W "${words}" -- "${cword}")
return 0
}
complete -F _act_completion act
@coderatchet
Copy link
Author

Place this in your ~/.bash_profile script.

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