unbtun python tab补全


在使用python的时候有时候总是忘记很多代码,这个是作为程序袁最头疼的事情,本人也是刚刚接触python,这几天也是用到这块,所以记录下来,已被需要时能够找到。
我的系统是:
 
w@w:~$ uname -a
Linux w 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

 

先看看自己的python安装路径,如果不知道的话可以根据下面的方法查看以下:
 
w@w:~$ python
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

 

既然找到了python的安装路径,把下面的代码传进去
 
#!/usr/bin/python
# python startup 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

 

 
所传路径以及文件名称可以随意,但是在调用的时候要一致。但是文件必须传到python路径下:
 
w@w:/usr/lib/python2.7/dist-packages$ vi tab.py 
#!/usr/bin/python
# python startup 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

 

 
我把文件传到/usr/lib/python2.7/dist-packages目录下,并且用tab.py命名。
 
w@w:~$ vi .bashrc
#for python    
export PYTHONSTARTUP=/usr/lib/python2.7/dist-packages/tab.py
#上面的路径和文件名必须和上面的保持一致。

 

 
w@w:~$ source .bashrc #启用上面的环境

评论关闭