如何在Ipython 5.0.0中设置特定的vim绑定 [英] How does one set specific vim-bindings in Ipython 5.0.0
问题描述
据我所知,因为Ipython 5.0.0使用新的输入库(prompt_toolkit),所以它不再默认为.inputrc(* nix)中指定的编辑器模式。必须在Ipython配置文件配置文件中设置此选项(请参阅 https://stackoverflow.com/a/38329940/2915339)。
I understand that because Ipython 5.0.0 uses a new input library (prompt_toolkit) it no longer defaults to the editor mode specified in .inputrc (*nix). This option has to be set in an Ipython profile configuration file (see https://stackoverflow.com/a/38329940/2915339).
我的问题是:在配置文件配置文件中设置了vi-mode,如何指定特定的键绑定?例如,我喜欢用'jk'来逃避。
My question is: having set vi-mode in the profile configuration file, how does one specify a particular keybinding? I like to use 'jk' for escape, for instance.
推荐答案
你是对的。 prompt_toolkit
忽略 .inputrc
。似乎没有办法在IPython 5.0.0配置文件配置文件中为 vi
模式定义自定义键绑定。
You're right. prompt_toolkit
ignores .inputrc
. There does not seem to be a way to define custom keybindings for the vi
mode in the IPython 5.0.0 profile configuration file.
这是我目前使用的解决方法。它不漂亮,但它现在有效。
Here's workaround I'm currently using. It's not pretty, but it works for now.
根据 IPython docs ,您可以在启动配置脚本中指定键盘快捷键。
According to the IPython docs, you can specify Keyboard Shortcuts in a startup configuration script.
而不是将 jk
重新绑定到 ESC
,我制作一个unicodej( u'j'
)后跟一个unicodek( u'k'
) VimInsertMode()
内部切换到导航模式的 prompt_toolkit
事件的快捷方式。
Instead of rebinding jk
to ESC
, I'm making a unicode "j" (u'j'
) followed by a unicode "k" (u'k'
) inside of VimInsertMode()
a shortcut for a prompt_toolkit
event that switches to navigation mode.
我使用以下代码创建了 .ipython / profile_default / startup / keybindings.py
:
I created a .ipython/profile_default/startup/keybindings.py
with the following code:
from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import HasFocus, ViInsertMode
from prompt_toolkit.key_binding.vi_state import InputMode
ip = get_ipython()
def switch_to_navigation_mode(event):
vi_state = event.cli.vi_state
vi_state.reset(InputMode.NAVIGATION)
if getattr(ip, 'pt_cli'):
registry = ip.pt_cli.application.key_bindings_registry
registry.add_binding(u'j',u'k',
filter=(HasFocus(DEFAULT_BUFFER)
& ViInsertMode()))(switch_to_navigation_mode)
prompt_toolkit源将帮助您根据需要实施其他快捷方式。
The prompt_toolkit source will help you implement other shortcuts as needed.
这篇关于如何在Ipython 5.0.0中设置特定的vim绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!