学习python,经常要使用python命令行查找一些不熟悉的使用方法等等,但是python命令行下没有自带tab补全的功能,看见别人写了tab,可以解决特此记下,以备后用
1.创建tab.py文件,内容如下:
#!/usr/bin/env 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
2.代码有了,我们还需要将脚本放到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目录下
3.把tab.py 放到指定目录下
mv tab.py /usr/local/lib/python2.7/dist-packages
4.使用
>>> 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( sys.maxint sys.subversion
sys.__setattr__( sys.exc_info( sys.maxsize sys.version
sys.__sizeof__( sys.exc_type sys.maxunicode sys.version_info
sys.__stderr__ sys.excepthook( sys.meta_path sys.warnoptions
sys.__stdin__ sys.exec_prefix sys.modules
sys.__stdout__ sys.executable sys.path
sys.__str__( sys.exit( sys.path_hooks