有什么方法可以替换IPython Vi模式中存在的默认键绑定吗? [英] Is there any way to replace the exists default key bindings in IPython Vi mode?
问题描述
我正在使用IPython Vi模式,并且IPython设置了许多默认键映射此处和此处,发现我可以创建新的使用KeyBindingManager
进行键绑定.
I am using IPython Vi mode, and IPython sets lots of default key mappings here. I did a research here and here, finding that I can create new key bindings by using KeyBindingManager
.
但是,如果我要创建的绑定存在,那么它将无法正常工作.例如,我在~/.ipython/profile_default/startup/vikeys.py
中编写一些代码:
However, if the binding I want to create exists, then it cannot works. For example, I write some code in ~/.ipython/profile_default/startup/vikeys.py
:
from prompt_toolkit.filters.cli import ViNavigationMode, ViMode
from prompt_toolkit.filters import Always, IsReadOnly
# create `handle`
from IPython import get_ipython
ip = get_ipython()
if getattr(ip, 'pt_cli'):
registry = ip.pt_cli.application.key_bindings_registry
handle = registry.add_binding
navigation_mode = ViNavigationMode() & ViMode() & Always()
@handle('t', filter=navigation_mode)
def _(event):
"""
Go up, but if we enter a new history entry, move to the start of the
line.
"""
event.current_buffer.auto_up(
count=event.arg, go_to_start_of_line_if_history_changes=True)
Since the t
is mapped here, my binding doesn't work.
我还尝试用下面的代码替换handle
,但没有效果:
I also tried to replace the handle
with the code below, but there was no effects:
# create `handle`
from prompt_toolkit.key_binding.manager import KeyBindingManager
manager = KeyBindingManager.for_prompt()
handle = manager.registry.add_binding
那么,有什么方法可以替换IPython中现有的键绑定吗?
So, is there any way to replace the existing key bindings in IPython?
推荐答案
我自己回答问题,以帮助遇到相同问题的人.
Answer the question myself, to help those who meet to same problem.
阅读源代码后,我发现在handle
上添加eager=True
参数将覆盖默认的键绑定.
After reading the source code, I find that adding eager=True
argument to the handle
will override the default keybindings.
此处是为Dvorak用户替换默认导航键的示例.
Here is an example to replace default navigation keys for Dvorak user.
这篇关于有什么方法可以替换IPython Vi模式中存在的默认键绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!