Skip to content

Instantly share code, notes, and snippets.

@fratajczak
Created November 16, 2022 23:36
Show Gist options
  • Save fratajczak/64e32421a43d3b8194d0409ce300518a to your computer and use it in GitHub Desktop.
Save fratajczak/64e32421a43d3b8194d0409ce300518a to your computer and use it in GitHub Desktop.
Fish-like word boundaries in ipython shortcuts

putting this file in ~/.ipython/profile_default/startup/shortcuts.py 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
registry.add_binding(Keys.ControlW,
filter=(has_focus(DEFAULT_BUFFER)
& ~has_selection
& emacs_insert_mode))(nc.backward_kill_word)
registry.add_binding(Keys.ShiftLeft,
filter=(has_focus(DEFAULT_BUFFER)
& emacs_insert_mode))(backward_word_space)
registry.add_binding(Keys.ShiftRight,
filter=(has_focus(DEFAULT_BUFFER)
& emacs_insert_mode))(forward_word_space)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment