As of IPython 5, readline
is no longer used to interpret keystrokes.
Instead, the pure-python library prompt_toolkit
is used.
Getting vim mode in IPython is straightforward. First, edit
~/.ipython/profile_default/ipython_config.py
and add the following line:
c.TerminalInteractiveShell.editing_mode = 'vi'
For custom keybindings, create a new file
~/.ipython/profile_default/startup/keybindings.py
and put this code in (this remaps jk to Esc):
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)
For notebooks, you can enable vim mode by installing this plugin https://github.com/lambdalisue/jupyter-vim-binding
This is not working anymore with newer version of
ipython
, see here for an updated code: https://ipython.readthedocs.io/en/stable/config/details.html#keyboard-shortcuts