据我所知,因为IPython的5.0.0采用了新的输入库(prompt_toolkit)不再默认为.inputrc文件(*nix中)指定的编辑模式.必须在Ipython配置文件配置文件中设置此选项(请参阅/sf/answers/2683095831/).
我的问题是:在配置文件配置文件中设置了vi-mode,如何指定特定的键绑定?例如,我喜欢用'jk'来逃避.
在IPython 5.0中,使用新的prompt_toolkit后端,如何修改或添加新的键盘快捷键?例如,我想让它成为历史记录中的向上箭头,而不是进行历史搜索(这是默认行为).
我正在为我的项目创建一个 REPL 工具,该工具(为了清楚起见而进行了简化)直接执行输入的命令,或者(如果输入命令“.x some/path/to/file”)从文件读取并执行它们。我的问题与自动完成用户输入(使用prompt_toolkit)有关。
我有类似的东西(最小可执行示例):
import prompt_toolkit
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.document import Document
from prompt_toolkit.contrib.completers import PathCompleter
class CommandCompleter(Completer):
def __init__(self):
self.path_completer = PathCompleter()
self.commands = [".x", "command1", "command2"]
def get_completions(self, document, complete_event):
if document.text.startswith(".x "):
sub_doc = Document(document.text[3:])
yield from (Completion(cmd.text, -document.cursor_position)
# ???????? ?????????????????????????
for cmd
in self.path_completer.get_completions(sub_doc, complete_event))
# ???????
else:
yield from (Completion(cmd, -document.cursor_position)
for cmd in self.commands
if cmd.startswith(document.text))
if __name__ == "__main__":
while True:
other_args = {}
input = prompt_toolkit.prompt(">>> ", …Run Code Online (Sandbox Code Playgroud) 在较旧的(我认为是5.0之前的版本)IPython中,如果我正在处理行/块,突然发现我需要研究其他事情来完成它,我的方法是按Ctrl-C,这留下了不完整的行/ block在屏幕上,但未执行,并给了我新的提示。也就是说,我会看到类似以下内容的内容:
In [1]: def foo():
...: stuff^C # <-- Just realized I needed to check something on stuff usage
In [2]: # <-- cursor goes to new line, but old stuff still on terminal
Run Code Online (Sandbox Code Playgroud)
在较新的IPython中(这似乎已经从切换readline到prompt_toolkit的“CLI支持框架”),Ctrl-C键的不同的行为; 现在,它没有给我换行符,而是重置了当前行,丢弃了我键入的所有内容,并将光标返回到该行的开头。
# Before:
In [1]: def foo():
...: stuff
# After Ctrl-C:
In [1]: # Hey, where'd everything go?
Run Code Online (Sandbox Code Playgroud)
这非常烦人,因为在完成任何辅助任务后,我再也无法看到或复制/粘贴正在处理的代码来恢复工作,因此需要重新提示。
我的问题是:有什么方法可以还原旧的IPython行为,其中Ctrl-C执行以下操作:
我到处搜索,发现最多的是一个错误报告注释,该注释提到此新行为是“ ...是对IPython早期版本的更改,但这是有意的。”
在IPython或prompt_toolkit文档中,我找不到任何有关修改行为的文档;我已经找到了许多安装了这些处理程序的位置,但是尝试用猴子补丁来更改当前行为却失败了(坦率地说,用猴子补丁来记录未记录的代码意味着我冒着破坏每次升级的风险,所以我想找到一些对此的半支持修复;否则,可以接受hacky的猴子修补程序)。
python keyboard-shortcuts ipython keyboardinterrupt prompt-toolkit