简体   繁体   English

ipython:更改 PageDown/PageUp 以通过命令历史记录向后/向前移动

[英]ipython: change PageDown/PageUp to move back/forward through command history

In my shell ( zsh ) or in python , I can go backward through command history by pressing PageDown , and I can go forward by pressing PageUp . In my shell ( zsh ) or in python , I can go backward through command history by pressing PageDown , and I can go forward by pressing PageUp .

But in ipython , these shortcuts are reversed.但是在ipython中,这些快捷方式是相反的。

Where are these shortcuts defined for ipython , and how can I reverse them back, so that这些为ipython定义的快捷方式在哪里,我怎样才能将它们反转回来,以便

PageDown goes back in history, and PageUp goes forward in history? PageDown回溯历史, PageUp历史前进?

I am using ipython3 version 5.8.0 on Debian 10.我在 Debian 10 上使用ipython3版本5.8.0

In IPython version 5.x, this is mentioned in the documentation: Specific config details — IPython 5.11.0.dev documentation在 IPython 版本 5.x 中,文档中提到了这一点: 特定配置详细信息 — IPython 5.11.0.dev 文档

To get the function to bind to, see key_binding/bindings/basic.py : The default is要获取要绑定的 function,请参见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"))

So, put this code in a startup file :因此,将此代码放在启动文件中:

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"))

On newer IPython versions (such as 7.19.0) replace the registry =... line with在较新的 IPython 版本(例如 7.19.0)上,将registry =...行替换为

registry = get_ipython().pt_app.key_bindings

Reference: Specific config details — IPython 7.19.0 documentation参考: 具体配置细节 — IPython 7.19.0 文档

Create script in ~/.ipython/profile_default/startup directory with any name ending with extension .py or .ipy~/.ipython/profile_default/startup目录中创建任何名称以扩展名.py.ipy结尾的脚本

For example i created history_keybindings.py and put it inside ~/.ipython/profile_default/startup directory例如,我创建了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)

Note: For more info check here注意:有关更多信息,请查看此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM