LoginSignup
2
1

More than 5 years have passed since last update.

Zshでhsenv.shを自動activate / deactivate

Posted at

この記事にはbash版しか載っていなかったのでzsh版を作った。

auto-hsenv
local search_dir=$PWD
while [[ "${search_dir}" != "/" ]]
do
    local hsenv_dir=""
    for dir in $(find ${search_dir} -maxdepth 1 -type d -name '.hsenv*')
    do
        if [[ -n "${HSENV}" && -e ${dir}/bin/activate ]]; then
            return
        elif [[ -z "${HSENV}" && -n "${hsenv_dir}" ]]; then
            echo "multiple environments in ${search_dir}."
            echo "activate manually"
            return
        elif [[ -z "${HSENV}" && -e ${dir}/bin/activate ]]; then
            hsenv_dir=${dir}
        fi
    done
    if [[ -z "${HSENV}" && -n "${hsenv_dir}" ]]; then
        source ${hsenv_dir}/bin/activate
        return
    fi
    search_dir=${search_dir}/..
    search_dir=${search_dir:a}
done
[[ -n "$HSENV" && "${search_dir}" == "/" ]] && deactivate_hsenv

これを FPATH の通った所に保存して .zshrc に以下のように追記すればOK。

autoload -Uz auto_hsenv && add-zsh-hook precmd auto_hsenv
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1