Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues setting up bash autocomplete for Python3 alias

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading