# vim:set ft=zsh pydirs="${PYENVS_DIR:-$XDG_DATA_HOME/virtualenv}" function chpwd_activate(){ [[ "$(pwd)" == "/" ]] && return 0 for pydir in $(ls $pydirs); do if [[ "$(pwd | sed -e 's|/|~|g' | cut -c2-)" =~ "^${pydir}$" ]] || [[ "r-$(pwd | sed -e 's|/|~|g' | cut -c2-)" =~ "^${pydir}(~.+)?$" ]]; then if [ "x$VIRTUAL_ENV" != "x$pydirs/$pydir" ]; then source "$pydirs/$pydir/bin/activate" fi return fi done if [ "x$VIRTUAL_ENV" != "x" ]; then deactivate fi } function venv(){ [[ "$(pwd)" == "/" ]] && echo "Cannot create venv at root" && return 1 [[ "$(pwd)" != "/" ]] && envdir=$(pwd | sed -e 's|/|~|g' | cut -c2-) [[ "$1" == "-r" ]] && envdir="r-$envdir" # create a venv that will be activated for all subdirectories as well if [ ! -e "$pydirs/${envdir}" ]; then echo "Creating python venv for ${envdir}.." mkdir "$pydirs" -p python3 -m venv "$pydirs/${envdir}" echo "Activating virtual env ${envdir}" source "$pydirs/${envdir}/bin/activate" else echo "A venv for this path already exists" fi } autoload -U add-zsh-hook add-zsh-hook chpwd chpwd_activate