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)
>Solution :
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.