18

Pip has a long list of commands. Is there any way to use auto-complete by Tab in console (Bash)?

6
  • nice! i it work for me like charm
    – Yurij
    Commented Apr 19, 2018 at 15:06
  • @Melebius that counts as an answer. Please answer, ping me and I'll come back to upvote!
    – Fabby
    Commented Apr 19, 2018 at 15:11
  • yes, this comment is a right answer for me.
    – Yurij
    Commented Apr 19, 2018 at 15:54
  • would you please change the accepted answer to askubuntu.com/a/1026594/470080 as it is now the correct answer @anonymous2 Commented Jun 13, 2021 at 21:17
  • @Julius I'm not the poster I simply edited the question; there's nothing I can do.
    – anonymous2
    Commented Jun 14, 2021 at 13:52

3 Answers 3

50

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.

7
  • For the moment the problem is solved with help of @Melebius. I would try your solution too, thanks.
    – Yurij
    Commented Apr 20, 2018 at 15:58
  • Funny that the completion does not install automatically together with pip if the code is contained in it…
    – Melebius
    Commented Apr 21, 2018 at 7:18
  • @Melebius there's no good way for it to do so - packages can't touch files in the user home directories, and installing globally to /etc/bash.bashrc is tricky to do automatically
    – muru
    Commented Apr 21, 2018 at 11:08
  • 1
    @muru As you can see in my answer and the linked page, there is also the directory /etc/bash_completion.d/ where it could be placed more easily.
    – Melebius
    Commented Apr 21, 2018 at 15:37
  • 1
    @Melebius which is from the bash-completion package, and those are two rather independent packages. This built-in completion code doesn't depend at all on bash-completion, so why would it use that directory?
    – muru
    Commented Apr 21, 2018 at 15:39
7

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
5
  • do you mind removing this answer as askubuntu.com/a/1026594/470080 is the correct answer now Commented Jun 13, 2021 at 21:18
  • @Julius Thanks for your reaction but at least 5 people have found this answer useful according to upvotes, so I don‘t think it’s necessary to completely remove it. I’ve added a link to the muru’s answer you mentioned. It’s up to the OP (Yurij) to update which answer he accepted.
    – Melebius
    Commented Jun 15, 2021 at 5:59
  • it was useful when it came out, it's not anymore. Are you so attached to your points Commented Jun 16, 2021 at 15:07
  • 1
    @Julius Even though pip provides a native solution, this answer is still relevant and there is NO good reason to suggest the author to remove it.
    – user545149
    Commented Nov 9, 2021 at 15:28
  • the reason is that it's not useful anymore, and it's still accepted, confusing people. At some point this will be all of code-oriented StackExchange, a million of accepted and upvoted answers that have been erroneous for years. Tell me how that is useful to anyone. Commented Nov 9, 2021 at 19:17
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .