在我的 shell( zsh
) 或 in 中python
,我可以按 后退命令历史记录PageDown,也可以按 前进PageUp。
但是在 中ipython
,这些捷径是相反的。
为 定义的这些快捷方式在哪里ipython
,如何将它们反转回来,以便
PageDown历史回溯,历史PageUp前行?
我在 Debian 10 上使用ipython3
版本。5.8.0
在我的 shell( zsh
) 或 in 中python
,我可以按 后退命令历史记录PageDown,也可以按 前进PageUp。
但是在 中ipython
,这些捷径是相反的。
为 定义的这些快捷方式在哪里ipython
,如何将它们反转回来,以便
PageDown历史回溯,历史PageUp前行?
我在 Debian 10 上使用ipython3
版本。5.8.0
在 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/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)
注意:有关更多信息,请查看此处