Automatically activate Poetry environments when changing directories in fish shell
This plugin automatically detects and activates Poetry virtual environments when you change into a Poetry project directory, and deactivates them when you leave. It's fast, efficient, and configurable.
- Automatic activation/deactivation of Poetry environments
- Smart caching to avoid slow
poetry env info
calls - Compatible with direnv and other environment managers
- Customizable with options to disable or enable verbose mode
- CLI management tool to control all aspects of auto-activation
Using Fisher
fisher install kelp/poetry-auto-fish
git clone https://github.com/kelp/poetry-auto-fish.git
cp -r poetry-auto-fish/functions/ ~/.config/fish/functions/
cp -r poetry-auto-fish/conf.d/ ~/.config/fish/conf.d/
cp -r poetry-auto-fish/completions/ ~/.config/fish/completions/
Once installed, poetry-auto-fish works immediately without any configuration. It will:
- Automatically detect Poetry projects (directories with
pyproject.toml
and[tool.poetry]
section) - Activate the appropriate virtual environment when you enter a project directory
- Deactivate the environment when you leave the project directory
The plugin provides a poetry-auto
command for control:
# Show current status
poetry-auto status
# Disable auto-activation
poetry-auto disable
# Enable auto-activation
poetry-auto enable
# Toggle verbose output (for debugging)
poetry-auto verbose
# Clear the environment cache
poetry-auto cache clear
You can configure the plugin by setting these variables in your config.fish
:
# Disable auto-activation
set -g POETRY_AUTO_DISABLE 1
# Enable verbose output
set -g POETRY_AUTO_VERBOSE 1
# Change cache directory
set -g POETRY_AUTO_CACHE_DIR "$HOME/.cache/custom-poetry-path"
poetry-auto-fish sets a POETRY_PROJECT
global variable with the active project name. You can use this in your prompt:
function fish_prompt
# Your existing prompt code
if set -q POETRY_PROJECT
echo -n " 📜($POETRY_PROJECT)"
end
# Rest of your prompt
end
The plugin uses smart caching to avoid repeated slow calls to poetry env info
. Once a virtual environment is activated, its path is cached using a directory hash, making future activations nearly instantaneous.
The plugin includes a test suite that can be run using the Makefile:
make test # Run tests
make lint # Check code style
make all # Run both tests and linting
make clean # Clean up test artifacts
The tests are automatically run on GitHub Actions for all pull requests and pushes to the main branch.
MIT © Travis Cole
Note: This plugin was largely developed with assistance from Claude Code, Anthropic's AI coding assistant.