Automatically detect and activate the nearest Python virtual environment when changing directories in zsh.
- Recursively searches from the current directory up to
$HOMEfor Python virtualenv folders - Honors common names:
.venv,venv,.env(configurable) - Activates on
cdand first prompt; deactivates when leaving the project - Skips scanning when outside
$HOME(configurable) - Respects user-managed venvs: if you manually activate a different venv, it won't override
Add this to your .zshrc:
# If this repo is local, use local bundle
antigen bundle /path-to-dir/zsh-auto-venv
# Or install from GitHub (recommended):
antigen bundle songlkkevin/zsh-auto-venv
antigen applyThe plugin file is zsh-auto-venv.plugin.zsh, so antigen will auto-load it.
Set any of these before loading the plugin (i.e., before antigen apply):
# Candidate directory names checked at each level
export AUTO_VENV_NAMES=".venv venv .env"
# Allow scanning outside $HOME (default: 0). Set to 1 to enable.
export AUTO_VENV_ENABLE_OUTSIDE_HOME=0
# Enable debug logs (default: 0)
export AUTO_VENV_DEBUG=0The plugin registers lightweight hooks on chpwd and precmd:
- When the directory changes (or on first prompt), it searches upward from
$PWD. - At each level, it checks for any directory in
AUTO_VENV_NAMEScontainingbin/activate. - If found, it activates that environment. If none is found and the plugin previously activated one, it deactivates it.
- If you manually activate a different venv, the plugin will not override it.
By default, the search stops at $HOME. If the current directory is not a subdirectory of $HOME, the plugin does nothing unless AUTO_VENV_ENABLE_OUTSIDE_HOME=1.
- Works with standard Python
venvandvirtualenvlayouts. - No external dependencies; pure zsh.
- For performance, only a small number of filesystem checks are done per directory level.
MIT — see LICENSE.