為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