Last active
August 17, 2016 00:05
-
-
Save coderatchet/21709ec7ce9cb9b73429eac46430dd35 to your computer and use it in GitHub Desktop.
bash completion for virtual environments (virtualenv) in mac os x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place this in your ~/.bash_profile script.