21

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

anonymous2
  • 4,325
Yurij
  • 605

3 Answers3

53

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.

muru
  • 207,970
  • For the moment the problem is solved with help of @Melebius. I would try your solution too, thanks. – Yurij Apr 20 '18 at 15:58
  • Funny that the completion does not install automatically together with pip if the code is contained in it… – Melebius Apr 21 '18 at 07: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 Apr 21 '18 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 Apr 21 '18 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 Apr 21 '18 at 15:39
  • @Yurij I checked it works. Also I think this should be the accepted answer since it is built-in in pip and doesn't require downloading git repository. – Izik Apr 23 '20 at 18:55
  • To try it out, or add it to your current shell without reloading your whole bashrc, use . <(pip completion --bash) – Walf Nov 09 '22 at 01: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
Melebius
  • 11,800
  • do you mind removing this answer as https://askubuntu.com/a/1026594/470080 is the correct answer now – Jules Gagnon-Marchand Jun 13 '21 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 Jun 15 '21 at 05:59
  • it was useful when it came out, it's not anymore. Are you so attached to your points – Jules Gagnon-Marchand Jun 16 '21 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. –  Nov 09 '21 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. – Jules Gagnon-Marchand Nov 09 '21 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.

User9123
  • 133