IPython 5.0 and key bindings in console

后端 未结 3 1227
死守一世寂寞
死守一世寂寞 2021-02-18 16:03

The new release of IPython does not depend any more on readline but uses the pure Python library prompt-toolkit, solving maintenance problems on Apple\

相关标签:
3条回答
  • 2021-02-18 16:17

    Modifying keyboard shortcuts in configuration when using prompt_toolkit is not (yet) possible; though it is pretty easy if you install IPython from source. If you look at the file IPython/terminal/shortcuts.py you will see that it contains the various logic; in particular you will find:

    # Ctrl+J == Enter, seemingly
    registry.add_binding(Keys.ControlJ,
                         filter=(HasFocus(DEFAULT_BUFFER)
                                 & ~HasSelection()
                                 & insert_mode
                        ))(newline_or_execute_outer(shell))
    

    This bind CtrlJ (enter) to the function newline_or_execute_outer which is responsible for adding new lines; it's define later in the file. In particular if you press enter twice at the end of a block of code, it should execute the block without the need to use any other shortcuts.

    Strip the logic that adds new lines:

    def execute_outer(shell):
        def execute(event):
            """When the user presses return, insert a newline or execute the code."""
            b = event.current_buffer
    
            # some logic to also dismiss the completer
    
            b.accept_action.validate_and_handle(event.cli, b)
        return execute
    

    Bind it around line 20-something:

    registry.add_binding(Keys.ControlE,
                         filter=(HasFocus(DEFAULT_BUFFER)
                                 & ~HasSelection()
                                 & insert_mode
                        ))(execute_outer(shell))
    

    And enjoy. If you are unhappy with the documentation we welcome help; For example, taking the gist of the answers there and contributing them back. It is a bit hurtful to read harsh comments when we do say in release notes:

    New terminal interface
    
    The overhaul of the terminal interface will probably cause a range of minor
    issues for existing users. This is inevitable for such a significant
    change, and we’ve done our best to minimise these issues. Some changes that
    we’re aware of, with suggestions on how to handle them:
    
    IPython no longer uses readline configuration (~/.inputrc). We hope that
    the functionality you want (e.g. vi input mode) will be available by
    configuring IPython directly (see Terminal IPython options). If something’s
    missing, please file an issue.
    
    ...
    

    Helping actually improving IPython to have configurable keybinding with actions name is also appreciated, so then you will be able to answer your own question.

    0 讨论(0)
  • 2021-02-18 16:18

    You could change xterm's configuration.

    xterm is configurable (and documented). In the xterm manual, the Default Key Bindings section shows the default binding for this key:

                            Alt <Key>Return:fullscreen() \n\
    

    You can suppress that binding in more than one way:

    • using the omitTranslation resource to suppress the feature
    • setting the fullscreen resource to never

    However, just suppressing it will not make it send something interesting (xterm ignores the modifier for Enter). Setting a translation resource works, e.g., in your $HOME/.Xdefaults file:

    *VT100*translations:      #override \n\ 
         Alt <Key>Return: string("\033[27;3;13~")
    
    0 讨论(0)
  • 2021-02-18 16:31

    The ctrl+j or ctrl+m keyboard shortcuts are validating the entry.

    0 讨论(0)
提交回复
热议问题