Lvnian:~ root# cat tab.py
#!/usr/bin/python
import readline,rlcompleter
### Indenting
class TabCompleter(rlcompleter.Completer):
"""Completer that supports indenting"""
def complete(self, text, state):
if not text:
return (' ', None)[state]
else:
return rlcompleter.Completer.complete(self, text, state)
readline.set_completer(TabCompleter().complete)
### Add autocompletion
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind -e")
readline.parse_and_bind("bind '\t' rl_complete")
else:
readline.parse_and_bind("tab: complete")
### Add history
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del histfile
Lvnian:~ root#
代码有了,我们还需要将脚本放到python指定的目录下,可以使用sys.path来查看一下
>>> 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/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下
mv tab.py /usr/local/lib/python2.7/dist-packages
ok,下面我们可以直接导入tab模块,马上体验一下tab补全吧.....
>>> import tab>>> sys.sys.__class__( sys.__subclasshook__( sys.exitfunc( sys.path_importer_cache
sys.__delattr__( sys._clear_type_cache( sys.flags sys.platform
sys.__dict__ sys._current_frames( sys.float_info sys.prefix
sys.__displayhook__( sys._getframe( sys.float_repr_style sys.ps1
sys.__doc__ sys._mercurial sys.getcheckinterval( sys.ps2
sys.__excepthook__( sys._multiarch sys.getdefaultencoding( sys.py3kwarning
sys.__format__( sys.api_version sys.getdlopenflags( sys.pydebug
sys.__getattribute__( sys.argv sys.getfilesystemencoding( sys.setcheckinterval(sys.__hash__( sys.builtin_module_names sys.getprofile( sys.setdlopenflags(sys.__init__( sys.byteorder sys.getrecursionlimit( sys.setprofile(sys.__name__ sys.call_tracing( sys.getrefcount( sys.setrecursionlimit(sys.__new__( sys.callstats( sys.getsizeof( sys.settrace(sys.__package__ sys.copyright sys.gettrace( sys.stderr
sys.__reduce__( sys.displayhook( sys.hexversion sys.stdin
sys.__reduce_ex__( sys.dont_write_bytecode sys.long_info sys.stdout
sys.__repr__( sys.exc_clear(