Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@

## Installation

Use [zplug](https://github.com/zplug/zplug) to install the plugin:
With [zplug](https://github.com/zplug/zplug):

```bash
zplug "Skylor-Tang/auto-venv", use:auto-venv.zsh, from:github, at:main
```

With [zpm](https://github.com/zpm-zsh/zpm):

```bash
zpm load Skylor-Tang/auto-venv
```

## Usage

The plugin automatically detects if there is a `venv` or `.venv` directory in the current directory or its parent directories, and activates the corresponding Python virtual environment.

When you change directories, the plugin will automatically detect and activate the virtual environment. If the current directory does not have a virtual environment, but you are currently in an active virtual environment, the plugin will automatically deactivate the current virtual environment.

## Silent Mode

By default, the plugin will print a message when it activates or deactivates a virtual environment. If you want to suppress these messages, you can set the `AUTO_VENV_SILENT` environment variable to `true`:

```bash
export AUTO_VENV_SILENT=true
```

## How it Works

The plugin uses the Zsh `chpwd` hook function to automatically call the `auto_venv` function when you change directories. This function recursively searches the parent directories until it finds a virtual environment directory or reaches the root directory or the user's home directory.
Expand Down
4 changes: 2 additions & 2 deletions auto-venv.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ auto_venv() {
# If a virtual environment directory is found, activate it
if [ -d "${local_venv_dir}" ]; then
source "${local_venv_dir}/bin/activate"
[ -t 1 ] && echo -e "\e[32mActivated Python virtual environment: ${local_venv_dir}\e[0m"
[ -t 1 ] && [ -z "$AUTO_VENV_SILENT" ] && echo -e "\e[32mActivated Python virtual environment: ${local_venv_dir}\e[0m"
return
fi
done
Expand All @@ -23,7 +23,7 @@ auto_venv() {
# deactivate the current virtual environment if it exists
if [[ "${current_dir}" == "/" || "${current_dir}" == "$HOME" ]]; then
if [[ -n "$VIRTUAL_ENV" ]]; then
[ -t 1 ] && echo -e "\e[31mDeactivated Python virtual environment: $VIRTUAL_ENV\e[0m"
[ -t 1 ] && [ -z "$AUTO_VENV_SILENT" ] && echo -e "\e[31mDeactivated Python virtual environment: $VIRTUAL_ENV\e[0m"
deactivate
fi
return
Expand Down