This repository was archived by the owner on Feb 10, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ source ${0: A: h} /zsh-auto-venv.zsh
2+
Original file line number Diff line number Diff line change 1+ function _activate() {
2+ local venv_dir=" $1 "
3+
4+ # Don't reactivate an already activated virtual environment
5+ if [[ -z " $VIRTUAL_ENV " || " $venv_dir " != " $$ VIRTUAL_ENV)" ]]; then
6+ source " $venv_dir /bin/activate"
7+ fi
8+ }
9+
10+ function _deactivate() {
11+ if [[ -n " $VIRTUAL_ENV " ]]; then
12+ deactivate
13+ fi
14+ }
15+
16+ # Gives the path to the nearest target file
17+ function _check_path()
18+ {
19+ local check_dir=" $1 "
20+ local check_file=" $2 "
21+
22+ if [[ -d " ${check_dir} /$check_file " ]]; then
23+ printf " ${check_dir} /$check_file "
24+ return
25+ else
26+ # Abort search at file system root or HOME directory (latter is a performance optimisation).
27+ if [[ " $check_dir " = " /" || " $check_dir " = " $HOME " ]]; then
28+ return
29+ fi
30+ _check_path " $( dirname " $check_dir " ) " " $check_file "
31+ fi
32+ }
33+
34+ function _check_venv()
35+ {
36+ local venv_path=" $( _check_path " $PWD " " venv" ) "
37+
38+ if [[ -n " $venv_path " ]]; then
39+ _activate " $venv_path "
40+ return
41+ else
42+ _deactivate
43+ fi
44+ }
45+
46+ autoload -Uz add-zsh-hook
47+ add-zsh-hook chpwd _check_venv
48+
49+ _check_venv
You can’t perform that action at this time.
0 commit comments