Linux python <tab>自动补全

为Python添加交互模式下TAB自动补全以及命令历史功能。

1.获取python目录

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6/site-packages/paramiko-1.7.7.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
>>> 

从上面看出python在我系统上的路径是  /usr/lib/python2.6/site-packages

2.切换至该目录写个tab.py的脚本,脚本目录就是处理python中<tab>事件,脚本内容如下

 [root@localhost]# cd /usr/lib/python2.6/site-packages

[root@localhost python2.6/]# vim tab.py

#!/usr/bin/python
# python tab file
  
import sys
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

3.切换至自己主目录

[root@localhost python2.6]# cd 
[root@localhost  ~]# vim .bashrc 

4. 增加环境变量

export PYTHONSTARTUP=/usr/lib/python2.6/site-packages/tab.py  

5.配置环境变量生效

[root@localhost ~]# source .bashrc  

  

  

  

 

 

 

 

 

 

 

 

 

  

posted @ 2016-07-28 15:11  梦徒  阅读(2453)  评论(0编辑  收藏  举报