在python2.x版本中,运行环境默认不支持tab键补全功能,而在python3.x中,默认是支持的。

python2.x版本中,设置的方法如下:

1.创建 tab.py 脚本,脚本内容如下:

1 [root@vcfs21 ~]# cat /usr/lib64/python2.7/tab.py
 2 #!/usr/bin/env python 
 3 # python startup file 
 4 import sys
 5 import readline
 6 import rlcompleter
 7 import atexit
 8 import os
 9 # tab completion 
10 readline.parse_and_bind('tab: complete')
11 # history file 
12 histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
13 try:
14     readline.read_history_file(histfile)
15 except IOError:
16     pass
17 atexit.register(readline.write_history_file, histfile)
18 del os, histfile, readline, rlcompleter

2.将 tab.py 文件复制或者移动到python的path路径目录下。

   path路径的确认方法如下:

1 [root@vcfs21 ~]# python
2 Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import sys
6 >>> sys.path
7 ['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
8 >>>

  将 tab.py 文件移动到 /usr/lib64/python2.7 目录:

1 [root@vcfs21 ~]# mv tab.py /usr/lib64/python2.7

3.将tab.py导入使用

1 [root@vcfs21 ~]# python
2 Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import tab