Linux下Python模式下[Tab]自动补全

Linux下Python模式,Tab自动补全
在Python模式交互下,tab自动补全会提高代码效率,通过以下步骤可以很方便的实现自动补全。

1.获取操作目录

[root@liu site-packages]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/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/gtk-2.0', '/usr/lib/python2.6/site-packages']
>>> 

可以看出,我的工作目录是/usr/lib/python2.6/site-packages/。

2.进入工作目录,编写tab.py补全文件

[root@liu site-packages]# cd /usr/lib/python2.6/site-packages/
[root@liu site-packages]# vim tab.py 

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@liu site-packages]# cd 
[root@liu ~]# vim .bashrc

在末尾添加一行

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

4.重读.bashrc文件

source .bashrc

或者

. .bashrc

5.测试效果

[root@liu ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math. 
math.__class__(         math.acos(              math.fsum(

.......
完成。我一开始一直报错,然后通过排查就是因为tab.py格式不正确。注意其格式。

原文:http://blog.csdn.net/u011456940/article/details/52754663

你可能感兴趣的:(Linux下Python模式下[Tab]自动补全)