0

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.

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.

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

So, is there any way to replace the existing key bindings in IPython?

zsrkmyn
  • 547
  • 1
  • 5
  • 20

1 Answers1

0

Answer the question myself, to help those who meet to same problem.

After reading the source code, I find that adding eager=True argument to the handle will override the default keybindings.

Here is an example to replace default navigation keys for Dvorak user.

zsrkmyn
  • 547
  • 1
  • 5
  • 20