Score:1

Issues setting up bash autocomplete for Python3 alias

cn flag

I've got an alias for python3, 'py' - it's set up as you might expect in my .bashrc file.

alias py='python3'
complete -F _python py

The issue is that complete will only work if I try to complete with the base python command python prior. Here's an example in the terminal log below:

~/askubuntodemo: ls                           
main.py

~/askubuntodemo: py -bash: completion: function `_python' not found

~/askubuntodemo: python main.py
hello world

~/askubuntodemo: py main.py
hello world

Is there a way I can initialise the autocomplete as soon as I log in as opposed to having to manually initiallise by doing a complete with the main python complete first.

(edit: for error in .bashrc while posting)

Score:1
vn flag

For autocomplete, I've solved this (with another alias, but modified here for python) by adding the following to /etc/bash_completion.d/bash_completion:

if [[ -r /usr/share/bash-completion/completions/python3 ]]; then
    . /usr/share/bash-completion/completions/python3 && complete -F _python python py
fi

I believe this could also be added to .bashrc, but both will be sourced so the result is the same.

Miku Tatukua avatar
cn flag
My bad - I miss-typed while writing the post. I will edit for future reference. I've just added your suggestion to the config file, and everything is working perfectly. Thanks a ton for the help.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.