您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页正文

新手必学的使用命令行实现tab键自动补全功能

2024-07-17 来源:意榕旅游网

具体实现步骤如下

1.编写具体的实现tab补全功能的脚本,名为tab.py

#!/usr/bin/python   
# python tab complete   

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的模块路径

>>> import sys  
>>> sys.path  
['', '/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/lib/python2.7/site-packages']  
>>>

python的模块放在了/usr/lib64/python27下面,将脚本拷贝到该目录下,在使用时导入即可。

3.导入tab

>>> import tab  

>>> import os  

>>> os.  

Display all 249 possibilities? (y or n)  

4.但python读取模块的路径顺序优先是从当前目录开始,所以若是当前目录也存在tab.py,但内容不同的python脚本,则可能会报错,所以在环境变量中也指定tab.py脚本

#for python  
export PYTHONSTARTUP=/usr/lib64/python2.7/tab.py

Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务