给Python IDLE加上自动补全和历史功能

作者: zwlzwy 2016-04-05 14:38:45
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import readline
import rlcompleter
import atexit
import os
# tab autocomplete
readline.parse_and_bind("tab: complete")
# history file
histfile = os.path.join(os.environ['HOME'], ".pythonhistory")
try:readline.read_history_file("histfile")
except IOError:pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

完成之后,我们把它保存为.pythonstartup.py,存放在自己的目录下(譬如/home/dir),再将PYTHONSTARTUP变量指向刚才放的地址,就可以了。最省事的办法是在bashrc中添加这样一行

export PYTHONSTARTUP=/home/dir/.pythonstartup.py

相关资讯