prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.19k stars 277 forks source link

Moving to previous/next word using Ctrl + Left/Right not possible in vi mode #123

Open dnivra opened 8 years ago

dnivra commented 8 years ago

Was this intentional or a bug? It works in emacs mode so thought I'd check. If it's intentional, how can I enable this in ptpython's configuration file? I'm running ptpython 0.34 from PyPI.

zlsun commented 8 years ago

Ctrl+Left/Right is only avaliable in emacs mode, it is intentional not a bug.

You can add following lines in ~/.ptpython/config.py to enable it in Vi insert mode:

    # Ctrl+Left: move to previous word beginning
    @repl.add_key_binding(Keys.ControlLeft, filter=ViInsertMode())
    def _(event):
        buffer = event.current_buffer
        pos = buffer.document.find_previous_word_beginning(count=event.arg)
        if pos:
            buffer.cursor_position += pos

    # Ctrl+Right: move to next word ending
    @repl.add_key_binding(Keys.ControlRight, filter=ViInsertMode())
    def _(event):
        buffer= event.current_buffer
        pos = buffer.document.find_next_word_ending(count=event.arg)
        if pos:
            buffer.cursor_position += pos
dnivra commented 8 years ago

Ah I see. Thanks for sharing the config @zlsun!

dkasak commented 7 years ago

Adding some emacs key bindings to a vi configuration or vice versa seems like a common use case. Is there a way to easily bind existing functions from another mode or do they need to be reimplemented manually like this?

For instance, I'd like to bind ControlA and ControlE to what they are in the emacs editing mode while I'm using vi mode.


To answer my own question, the module prompt_toolkit.key_binding.bindings.named_commands contains many predefined commands, so putting this in your ptpython/config.py works:

import prompt_toolkit.key_binding.bindings.named_commands as named
[...]
def configure(repl):
    [...]
    repl.add_key_binding(Keys.ControlA)(named.beginning_of_line)
    repl.add_key_binding(Keys.ControlE)(named.end_of_line)
jonathanslenders commented 7 years ago

@dkasak, I tried to implement as much as possible key handlers from readline, using their name: https://github.com/jonathanslenders/python-prompt- toolkit/blob/master/prompt_toolkit/key_binding/bindings/named_commands.py These can be bound to any key. The idea is that in the end we should even be able to parse an inputrc file and map the bindings from that file. That's still some way to go, but that's the idea.