簡體   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 .

但是在ipython中,這些快捷方式是相反的。

這些為ipython定義的快捷方式在哪里,我怎樣才能將它們反轉回來,以便

PageDown回溯歷史, PageUp歷史前進?

我在 Debian 10 上使用ipython3版本5.8.0

在 IPython 版本 5.x 中,文檔中提到了這一點: 特定配置詳細信息 — IPython 5.11.0.dev 文檔

要獲取要綁定的 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"))

因此,將此代碼放在啟動文件中:

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 文檔

~/.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)

注意:有關更多信息,請查看此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM