zsh-uv-env is a plugin for zsh and uv. It automatically activates a virtual environment based on the current directory.
-
Clone this repository into
$ZSH_CUSTOM/plugins
(by default~/.oh-my-zsh/custom/plugins
)git clone https://github.com/matthiasha/zsh-uv-env ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-uv-env
-
Add the plugin to the list of plugins for Oh My Zsh to load (inside
~/.zshrc
):plugins=( ... zsh-uv-env )
-
Start a new terminal session.
The plugin automatically detects and activates Python virtual environments (.venv directories) as you navigate through your filesystem. When you leave a directory with an active virtual environment, it automatically deactivates it.
This plugin supports post-hooks that allow you to execute custom commands after a virtual environment is activated or deactivated.
You can add post-hooks in your .zshrc file:
# Define hook functions
_venv_post_hook() {
v cd
v venv
}
_venv_post_hook_deactivate() {
v cd
v reset_venv
}
# Register the hooks
zsh_uv_add_post_hook_on_activate '_venv_post_hook'
zsh_uv_add_post_hook_on_deactivate '_venv_post_hook_deactivate'
Then call this function in your .zshrc.
zsh_uv_add_post_hook_on_activate: Register a function to run after a virtual environment is activated
zsh_uv_add_post_hook_on_deactivate: Register a function to run after a virtual environment is deactivated