How to set specific vim bindings in ipython 5.0.0

I understand that since Ipython 5.0.0 uses the new input library (prompt_toolkit), it no longer uses the editor mode specified in .inputrc (* nix). This parameter must be set in the Ipython profile configuration file (see https://stackoverflow.com/a/167449/ ).

My question is: has vi mode been set in the profile configuration file, how to define a specific keybinding? For example, I like to use jk for escape.

+5
python linux vim ipython prompt-toolkit
Jul 18 '16 at 18:43
source share
1 answer

You're right. prompt_toolkit ignores .inputrc . There seems to be no way to define custom key bindings for vi mode in the IPython 5.0.0 profile configuration file.

Here's the workaround I'm using now. This is not very, but now it works.

According to the IPython docs, you can specify keyboard shortcuts in the startup script configuration.

Instead of binding jk to ESC , I make unicode "j" ( u'j' ) and then unicode "k" ( u'k' ) inside VimInsertMode() shortcut for prompt_toolkit , which switches to navigation mode.

I created .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) 

The prompt_toolkit source will help you use other shortcuts if necessary.

+6
Aug 07 '16 at 4:12
source share



All Articles