python——使用readline库实现tab自动补全
Input History
readline tracks the input history automatically. There are two different sets of functions for working with the history. The history for the current session can be accessed with get_current_history_length()and get_history_item(). That same history can be saved to a file to be reloaded later using write_history_file() and read_history_file(). By default the entire history is saved but the maximum length of the file can be set with set_history_length(). A length of -1 means no limit.
import readline
import logging
import os LOG_FILENAME = '/tmp/completer.log'
HISTORY_FILENAME = '/tmp/completer.hist' logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
) def get_history_items():
return [ readline.get_history_item(i)
for i in xrange(1, readline.get_current_history_length() + 1)
] class HistoryCompleter(object): def __init__(self):
self.matches = []
return def complete(self, text, state):
response = None
if state == 0:
history_values = get_history_items()
logging.debug('history: %s', history_values)
if text:
self.matches = sorted(h
for h in history_values
if h and h.startswith(text))
else:
self.matches = []
logging.debug('matches: %s', self.matches)
try:
response = self.matches[state]
except IndexError:
response = None
logging.debug('complete(%s, %s) => %s',
repr(text), state, repr(response))
return response def input_loop():
if os.path.exists(HISTORY_FILENAME):
readline.read_history_file(HISTORY_FILENAME)
print 'Max history file length:', readline.get_history_length()
print 'Startup history:', get_history_items()
try:
while True:
line = raw_input('Prompt ("stop" to quit): ')
if line == 'stop':
break
if line:
print 'Adding "%s" to the history' % line
finally:
print 'Final history:', get_history_items()
readline.write_history_file(HISTORY_FILENAME) # Register our completer function
readline.set_completer(HistoryCompleter().complete) # Use the tab key for completion
readline.parse_and_bind('tab: complete') # Prompt the user for text
input_loop()
The HistoryCompleter remembers everything you type and uses those values when completing subsequent inputs.
$ python readline_history.py
Max history file length: -1
Startup history: []
Prompt ("stop" to quit): foo
Adding "foo" to the history
Prompt ("stop" to quit): bar
Adding "bar" to the history
Prompt ("stop" to quit): blah
Adding "blah" to the history
Prompt ("stop" to quit): b
bar blah
Prompt ("stop" to quit): b
Prompt ("stop" to quit): stop
Final history: ['foo', 'bar', 'blah', 'stop']
The log shows this output when the “b” is followed by two TABs.
DEBUG:root:history: ['foo', 'bar', 'blah']
DEBUG:root:matches: ['bar', 'blah']
DEBUG:root:complete('b', 0) => 'bar'
DEBUG:root:complete('b', 1) => 'blah'
DEBUG:root:complete('b', 2) => None
DEBUG:root:history: ['foo', 'bar', 'blah']
DEBUG:root:matches: ['bar', 'blah']
DEBUG:root:complete('b', 0) => 'bar'
DEBUG:root:complete('b', 1) => 'blah'
DEBUG:root:complete('b', 2) => None
When the script is run the second time, all of the history is read from the file.
$ python readline_history.py
Max history file length: -1
Startup history: ['foo', 'bar', 'blah', 'stop']
Prompt ("stop" to quit):
There are functions for removing individual history items and clearing the entire history, as well.
最后效果是你输入的历史的foo被记录了下来,下次打开的时候输入fo然后tab就可以自动提示foo来补全了!
python——使用readline库实现tab自动补全的更多相关文章
- Linux python <tab>自动补全
为Python添加交互模式下TAB自动补全以及命令历史功能. 1.获取python目录 [root@localhost ~]# python Python 2.6.6 (r266:84292, Jul ...
- Python建立Tab自动补全的脚本
Python建立Tab自动补全的脚本 #!/usr/bin/python #python steup file import sys import readline import rlcomplete ...
- Python-2.7 配置tab自动补全功能
作者博文地址:http://www.cnblogs.com/spiritman/ 之前一直使用shell编程,习惯了shell的 tab 自动补全功能,而Python的命令行却不支持 tab 自动补全 ...
- Python-2.7 配置 tab 自动补全功能
作者博文地址:http://www.cnblogs.com/liu-shuai/ 之前一直使用shell编程,习惯了shell的 tab 自动补全功能,而Python的命令行却不支持 tab 自动补全 ...
- 如何为 .NET Core CLI 启用 TAB 自动补全功能
如何为 .NET Core CLI 启用 TAB 自动补全功能 Intro 在 Linux 下经常可以发现有些目录/文件名,以及有些工具可以命令输入几个字母之后按 TAB 自动补全,最近发现其实 do ...
- Mysql命令行tab自动补全方法
在mysql命令行有时为了方便想要按tbl键自动补全命令,以便节约时间. 具体方法如下: 第一步:修改my.cnf vi mysql/etc/my.cnf 将下图红框的代码注释,修改成如下代码: #d ...
- sudo和man的tab自动补全
要加入sudo和man的tab自动补全功能,只需在~/.bashrc中加入: #Enabling tab-completioncomplete -cf sudocomplete -cf man
- Windows 下python的tab自动补全
方法一:安装一个ipython就OK啦,而且关键字还能高亮显示呢 一.打开cmd,输入pip3 install ipython联网安装 二.安装成功后,cmd里运行ipython,成功啦. 方法二:写 ...
- Mac Tab自动补全键
最近入手一个Mac(Mac 2019版本),在使用终端时,发现不能使用Tab键自动补全代码,网络搜寻下,发现这里有个方法,记录下,免得自己忘记: 1 / 首先找到这个图标 2 / 输入命令 nano ...
随机推荐
- STORM_0001_用vmware拷贝出三个相同的ubuntu搭建小的zookeeper集群
第一次配置zookeeper的集群 因为想运行storm必须搭建集群在自己的电脑上拷贝了自己的ubuntu虚拟机采用的是vmware给虚拟机分配的地址三个机器的配置基本上一样除了myid这个文件看了这 ...
- Java 实现二分法查找算法
算法 假如有一组数为3,12,24,36,55,68,75,88要查给定的值24.可设三个变量front,mid,end分别指向数据的上界,中间和下界,mid=(front+end)/2. 1.开始令 ...
- Scrum Meeting--Twelve(2015-11-3)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 服务器修改与优化 5h 服务器修改与优化 4h 胡亚坤 客户端数据更新 2h 客户端意见反馈收集 2 ...
- jdk和eclipse位数不一致出错
32位的eclipse无法打开:找不64位jdk6的jvm.dll文件(64位的没有这个文件).网上说法可以通过设置eclipse初始化文件xxx.ini改变方式: 直接换成了同位数的了,没去试了.
- QQReg.java
import java.awt.*; import javax.swing.*; public class QQReg extends JFrame{ public static void main( ...
- EF执行存储过程
//执行strSql/procSql //返回受影响的行数 int i = dbsql.Database.ExecuteSqlCommand("exec getActionUrlId @na ...
- Python学习(7)数字
目录 Python 数字 Python 数字类型转换 Python 数学函数 Python 随机数函数 Python 三角函数 Python 数学常量 Python 数字 Python 数字数据类型用 ...
- boost中的智能指针
进行本地线程管理的 thread_specific_ptr 指针: 可以看这里:http://www.kingofcoders.com/viewNews.php?type=newsCpp&id ...
- (一)S5PV210开发板常用易忘操作记录
一.调试串口 2.SD卡槽 (三)启动方式选择 蜂鸣器下面的白色2针插座(图中红色线圈出来的那个)为选择USB/SD卡启动的开关.默认情况下为SD卡启动,如果需要USB启动则使用短路帽(若没有短路帽, ...
- Android提高篇之自定义dialog实现processDialog“正在加载”效果、使用Animation实现图片旋转
知识点: 1.使用imageview.textview自定义dialog 2.使用Animation实现图片旋转动画效果 3.通过自定义theme去掉dialog的title 没有使用progres ...