Pip has a long list of commands. Is there any way to use auto-complete by Tab in console (Bash)?
3 Answers
A reasonably current pip comes with built-in functionality to create completion helpers for bash, zsh or fish:
$ pip help completion
Usage: pip completion [options]
Description:
A helper command to be used for command completion.
Completion Options:
-b, --bash Emit completion code for bash
-z, --zsh Emit completion code for zsh
-f, --fish Emit completion code for fish
You can use it like so:
pip completion --bash >> ~/.bashrc
And then start a new shell or source ~/.bashrc to have it take effect.
Or to have it load on demand rather than in every shell, do:
mkdir -p ~/.local/share/bash-completion/completions/
pip completion --bash > ~/.local/share/bash-completion/completions/pip
and source that. This has the advantage of being able to run the same command again at any time to update it, rather than having to edit your main .bashrc.
- 207,228
UPDATE: Don’t forget to look at muru’s answer which may provide a more straightforward solution.
A pip autocompletion plugin for Bash can be found at https://github.com/ekalinin/pip-bash-completion.
You can download it as a ZIP or simply install using Git:
git clone https://github.com/ekalinin/pip-bash-completion.git
sudo cp ./pip-bash-completion/pip /etc/bash_completion.d/
. /etc/bash_completion.d/pip # to enable in the current shell, next time should load automatically
- 11,750
I would like to complete muru’s answer answer.
I like to keep my .bashrc relatively clean, plus if you use the following code, you won't have to update your .bashrc if/when the completion snippet has an update!
Just add this single line to your .bashrc:
eval "$(pip completion --bash)"
Also, you might want to add that line too, because the completion is only added to pip, not pip3:
complete -o default -F _pip_completion pip3
UPDATE: Running the command each time a new terminal is open is actually slow. It takes from 1 to 5 seconds on my PC. I used muru’s answer at the end.
- 133
- 6