pipenv.plugin.zsh
2655 bytes
1if (( ! $+commands[pipenv] )); then
2 return
3fi
4
5# Compatibility note:
6# pipenv < 2026.5.0 used Click-based shell completion driven by the
7# _PIPENV_COMPLETE environment variable.
8#
9# pipenv >= 2026.5.0 removed this mechanism and switched to argcomplete-based
10# completion using register-python-argcomplete instead.
11
12autoload -Uz is-at-least
13
14_pipenv_version_cache="$ZSH_CACHE_DIR/pipenv_version"
15
16if [[ -f "$_pipenv_version_cache" ]]; then
17 _pipenv_version="$(< "$_pipenv_version_cache" 2>/dev/null)"
18else
19 _pipenv_version="${$(pipenv --version 2>/dev/null)#pipenv, version }"
20fi
21
22{
23 _pipenv_fresh_version="${$(pipenv --version 2>/dev/null)#pipenv, version }"
24 [[ -n "$_pipenv_fresh_version" ]] && print -r -- "$_pipenv_fresh_version" >| "$_pipenv_version_cache"
25} &|
26
27if is-at-least 2026.5.0 "$_pipenv_version"; then
28 # pipenv >= 2026.5.0: argcomplete-based completion (no legacy fallback)
29 if (( $+commands[register-python-argcomplete] )); then
30 autoload -Uz bashcompinit
31 bashcompinit
32
33 eval "$(register-python-argcomplete pipenv)"
34 fi
35else
36 # legacy Click-based completion via _PIPENV_COMPLETE
37
38 # If the completion file doesn't exist yet, we need to autoload it and
39 # bind it to `pipenv`. Otherwise, compinit will have already done that.
40 if [[ ! -f "$ZSH_CACHE_DIR/completions/_pipenv" ]]; then
41 typeset -g -A _comps
42 autoload -Uz _pipenv
43 _comps[pipenv]=_pipenv
44 fi
45
46 _PIPENV_COMPLETE=zsh_source pipenv >| "$ZSH_CACHE_DIR/completions/_pipenv" &|
47fi
48
49if zstyle -T ':omz:plugins:pipenv' auto-shell; then
50 # Automatic pipenv shell activation/deactivation
51 _togglePipenvShell() {
52 # deactivate shell if Pipfile doesn't exist and not in a subdir
53 if [[ ! -f "$PWD/Pipfile" ]]; then
54 if [[ "$PIPENV_ACTIVE" == 1 ]]; then
55 if [[ "$PWD" != "$pipfile_dir"* ]]; then
56 unset PIPENV_ACTIVE pipfile_dir
57 deactivate
58 fi
59 fi
60 fi
61
62 # activate the shell if Pipfile exists
63 if [[ "$PIPENV_ACTIVE" != 1 ]]; then
64 if [[ -f "$PWD/Pipfile" ]]; then
65 export pipfile_dir="$PWD"
66 source "$(pipenv --venv)/bin/activate"
67 export PIPENV_ACTIVE=1
68 fi
69 fi
70 }
71
72 autoload -U add-zsh-hook
73 add-zsh-hook chpwd _togglePipenvShell
74 _togglePipenvShell
75fi
76
77# Aliases
78alias pch="pipenv check"
79alias pcl="pipenv clean"
80alias pgr="pipenv graph"
81alias pi="pipenv install"
82alias pidev="pipenv install --dev"
83alias pl="pipenv lock"
84alias po="pipenv open"
85alias prun="pipenv run"
86alias psh="pipenv shell"
87alias psy="pipenv sync"
88alias pu="pipenv uninstall"
89alias pupd="pipenv update"
90alias pwh="pipenv --where"
91alias pvenv="pipenv --venv"
92alias ppy="pipenv --py"