Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2022 23:36
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Fish-like word boundaries in ipython shortcuts

putting this file in ~/.ipython/profile_default/startup/ will change bindings for ctrl-w and shift+left/right arrow to match fish's behaviour regarding word boundaries.

from prompt_toolkit.key_binding.bindings import named_commands as nc
from prompt_toolkit.keys import Keys
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import has_focus, has_selection, emacs_insert_mode
ipython = get_ipython()
def backward_word_space(event):
buf = event.current_buffer
pos = buf.document.find_previous_word_beginning(count=event.arg, WORD=True)
if pos:
buf.cursor_position += pos
def forward_word_space(event):
buf = event.current_buffer
pos = buf.document.find_next_word_ending(count=event.arg, WORD=True)
if pos:
buf.cursor_position += pos
if getattr(ipython, 'pt_app', None):
registry = ipython.pt_app.key_bindings
& ~has_selection
& emacs_insert_mode))(nc.backward_kill_word)
& emacs_insert_mode))(backward_word_space)
& emacs_insert_mode))(forward_word_space)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment