bash-it/completion/available/pip3.completion.bash

38 lines
1.2 KiB
Bash

# https://pip.pypa.io/en/stable/user_guide/#command-completion
# Of course, you should first install the pip, say on Debian:
# sudo apt-get install python-pip
# sudo apt-get install python3-pip
# If the pip package is installed within virtual environments,
# you should first install pip for the corresponding environment.
# Fix pip completion for running it within/outside of pyenv/virtualenv/venv/conda environment.
_regex="(^|[ ]|;)_pip_completion_hook(;|[ ]|$)"
_pip_completion_hook() {
local _pip
# For pip resides within the pyenv/virtualenv/venv/conda environments:
if [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/pip3" ]; then
_pip="$VIRTUAL_ENV/bin/pip3"
elif [ -n "$CONDA_PREFIX" ] && [ -x "$CONDA_PREFIX/bin/pip3" ]; then
_pip="$CONDA_PREFIX/bin/pip3"
# For pip resides outside of a virtual environment:
elif [ -x /usr/bin/pip3 ]; then
_pip=/usr/bin/pip3
fi
if [ -n "$_pip" ]; then
eval "$($_pip completion --bash)"
fi
unset _pip
}
if [ -z "$PROMPT_COMMAND" ]; then
PROMPT_COMMAND=_pip_completion_hook
elif ! [[ "$PROMPT_COMMAND" =~ $_regex ]]; then
PROMPT_COMMAND="_pip_completion_hook;$PROMPT_COMMAND"
fi
unset _regex