在Linux系统系自带的python shell 模式下支持tab键补全功能

官方文档的Example如下

>>> import rlcompleter  
>>> import readline  
>>> readline.parse_and_bind("tab: complete")  
>>> readline.   
readline.__doc__          readline.get_line_buffer(  readline.read_init_file(  
readline.__file__         readline.insert_text(      readline.set_completer(  
readline.__name__         readline.parse_and_bind(  
...  

只要import rlcompleter和readline这两个模块之后,再对readline设置一下就行,但是每次重启后都要输入一次,太麻烦。

幸好,文档里说设置PYTHONSTARTUP环境变量便可以在python sehll启动前设置:

  • 打开~/.bashrc 或者man bash查看一下bash的变量设置文件
    添加如下命令:
export PYTHONSTARTUP=~/.pythonst.py
  • 并在当前目录下创建 .pythonst.py文件
# This is python shell startup script                                 

# for Tab auto complete  
print "Python start up..."  
try:  
    import readline  
except ImportError:  
    print "Module readline not available."  
else:  
    import rlcompleter  
    readline.parse_and_bind("tab: complete")

保存后重启terminal即可。本文章转自

你可能感兴趣的:(在Linux系统系自带的python shell 模式下支持tab键补全功能)