python实现Tab自动补全功能

@lianst  November 20, 2016

{大神请忽略}

默认情况下python命令行下是不支持命令补全功能的,用的时候总是得百度,所以索性在博客简单记录一下,方便下次使用.

一、创建tab.py脚本

1.1 创建脚本tab.py

#!/usr/bin/env python
 
import sys
import readline
import rlcompleter
import atexit
import os
 
readline.parse_and_bind('tab: complete')
 
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

二、安装tab.py脚本

2.1 进入python查看python的安装路径

2.2 复制/剪切tab.py到安装目录下
一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下.

cp tab.py /usr/lib/python2.7/dist-packages/

三、测试Tab补全

Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab  //导入tab模块
>>> pri
print
>>> print

>>> import sys
>>> sys.
sys.__class__(              sys.exit(
sys.__delattr__(            sys.exitfunc(
sys.__dict__                sys.flags
sys.__displayhook__(        sys.float_info
sys.__doc__                 sys.float_repr_style
sys.__excepthook__(         sys.getcheckinterval(
sys.__format__(             sys.getdefaultencoding(
sys.__getattribute__(       sys.getdlopenflags(
sys.__hash__(               sys.getfilesystemencoding(
sys.__init__(               sys.getprofile(
sys.__name__                sys.getrecursionlimit(
.....


添加新评论