Skip to content

Instantly share code, notes, and snippets.

@pymen
Forked from tek/virtualenv.zsh
Created December 15, 2013 13:05
Show Gist options
  • Save pymen/7972786 to your computer and use it in GitHub Desktop.
Save pymen/7972786 to your computer and use it in GitHub Desktop.
virtualEnv with zsh hooks
typeset -A virtualenv_map
virtualenv_map=()
for file in ~/.virtualenvs/*/.project(N)
virtualenv_map[${file:h:t}]=$(<$file)
virtualenv_chpwd()
{
setopt local_options no_auto_pushd
local project_path longest_path new_venv
if [[ $ZSH_SUBSHELL == 0 ]] && \
type workon &>/dev/null && \
[[ -z $activating_venv ]]; then
for name in ${(k)virtualenv_map}; do
project_path=$virtualenv_map[$name]
if [[ $PWD =~ $project_path ]] && \
[[ $#project_path > $#longest_path ]]; then
new_venv=$name
longest_path=$project_path
fi
done
if [[ -n $new_venv ]] && \
[[ $new_venv != $current_virtualenv ]]; then
current_virtualenv=$new_venv
activating_venv=1
print "Activating virtualenv $current_virtualenv..."
workon $current_virtualenv
unset activating_venv
elif [[ -n $current_virtualenv ]] && \
[[ ! $PWD =~ ${virtualenv_map[$current_virtualenv]} ]]; then
print "Deactivating virtualenv $current_virtualenv..."
unset current_virtualenv
deactivate
fi
fi
}
add-zsh-hook chpwd virtualenv_chpwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment