[tip]为python增加自动补全和历史记忆功能
Posted in Tech on
2009/10/19 – 18:51
Post a comment
for linux
主目录下新建一个.pythonstartup的文件,内容如下:
# python startup file
import readline
import rlcompleter
import atexit
import os
# tab completion
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
import readline
import rlcompleter
import atexit
import os
# tab completion
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
如果这时还没生效,可以在.bashrc或者.profile文件中加入
export PYTHONSTARTUP= ~/.pythonstartup
for win
安装readline,下载地址
在任意目录,比如D:\Python2.5下建立一个文件startup.py,内容如下:
# python startup file
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.path.dirname(os.environ['PYTHONSTARTUP']), '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.path.dirname(os.environ['PYTHONSTARTUP']), '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
增加环境变量PYTHONSTARTUP,值为刚才那个文件的路径。
如果你喜欢这篇文章,欢迎 订阅我的 RSS feed!




