3

在我的 shell( zsh) 或 in 中python,我可以按 后退命令历史记录PageDown,也可以按 前进PageUp

但是在 中ipython,这些捷径是相反的。

为 定义的这些快捷方式在哪里ipython,如何将它们反转回来,以便

PageDown历史回溯,历史PageUp前行?

我在 Debian 10 上使用ipython3版本。5.8.0

4

2 回答 2

1

在 IPython 版本 5.x 中,文档中提到了这一点:特定配置详细信息 — IPython 5.11.0.dev 文档

要获取要绑定的函数,请参阅key_binding/bindings/basic.py:默认为

handle("pageup", filter=~has_selection)(get_by_name("previous-history"))
handle("pagedown", filter=~has_selection)(get_by_name("next-history"))

因此,将此代码放在启动文件中:

from IPython import get_ipython
from prompt_toolkit.filters import HasSelection
from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.bindings.named_commands import get_by_name

registry = get_ipython().pt_cli.application.key_bindings_registry
registry.add_binding(Keys.PageUp, filter=~HasSelection())(get_by_name("next-history"))
registry.add_binding(Keys.PageDown, filter=~HasSelection())(get_by_name("previous-history"))

在较新的 IPython 版本(例如 7.19.0)上,将该registry = ...行替换为

registry = get_ipython().pt_app.key_bindings

参考:具体配置细节 — IPython 7.19.0 文档

于 2021-01-31T17:14:00.357 回答
1

~/.ipython/profile_default/startup在目录中创建任何名称以扩展名结尾的脚本.py.ipy

例如我创建history_keybindings.py并将其放在~/.ipython/profile_default/startup目录中

from IPython import get_ipython
from IPython.terminal.shortcuts import previous_history_or_previous_completion, next_history_or_next_completion
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import HasSelection

ip = get_ipython()

registry = None

if (getattr(ip, 'pt_app', None)):
   # for IPython versions 7.x
   registry = ip.pt_app.key_bindings
elif (getattr(ip, 'pt_cli', None)):
   # for IPython versions 5.x
   registry = ip.pt_cli.application.key_bindings_registry

if registry:
   registry.add_binding(Keys.PageUp, filter=(~HasSelection()))(previous_history_or_previous_completion)
   registry.add_binding(Keys.PageDown, filter=(~HasSelection()))(next_history_or_next_completion)

注意:有关更多信息,请查看此处

于 2021-02-01T03:58:46.843 回答