How can I get tab-completion in the interactive Python interpreter?
First, create a new file called .pythonstartup.py
in your home directory. Put the following script in it:
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
The parentheses around the string ensure that it works with both Python 2 and Python 3.
Every time the interactive interpreter is started, it executes a script defined in $PYTHONSTARTUP
, if there is one. To set it to execute the above script, type
export PYTHONSTARTUP="~/.pythonstartup.py"
You should write this line to your .bashrc
or .bash_profile
file, so that it's automatically executed when a new shell is started.