Xonsh: How to bind acceptance of automatic command suggestion (auto_suggest) to a different key or keys than right-arrow

Created on 1 Apr 2020  路  4Comments  路  Source: xonsh/xonsh

I am wondering if there is a way to configure what key or keys are bound to the acceptance of the automatic command suggestion. The default keybinding is right-arrow. The only documentation I found around this is actually in the $AUTO_SUGGEST environment variable. (Even the main xonsh tutorial doesn't describe how to accept the suggested commands; documentation of this feature could perhaps be described in the section "Up, Down, Tab").

I checked both the Programmable Tab-Completion tutorial as well as the prompt_toolkit Custom Keybindings tutorial, but didn't really get a good idea of what I'd need to do. I also checked the Prompt Toolkit docs on Auto suggestion, but again, didn't really get an idea of how to control accepting an auto-suggestion.

prompt-toolkit question

All 4 comments

I use ctrl-e for the same...

Hey @gotgenes -- no promises that this won't break in the future, but this should work (here I'm binding Ctrl-B to accept the autosuggestion):

from prompt_toolkit.filters import Condition
from prompt_toolkit.keys import Keys
from prompt_toolkit.application.current import get_app

@events.on_ptk_create
def custom_keybindings(bindings, **kw):
    handler = bindings.add

    @Condition
    def suggestion_available():
        app = get_app()
        return (
            app.current_buffer.suggestion is not None
            and app.current_buffer.document.is_cursor_at_the_end
        )

    @handler(Keys.ControlB, filter=suggestion_available)
    def accept_auto_suggestion(event):
        b = event.current_buffer
        suggestion = b.suggestion

        if suggestion:
            b.insert_text(suggestion.text)

Most of the time to rebind existing prompt toolkit functionality, you need to first find the existing PTK code. I cribbed this from here:
https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/auto_suggest.py#L29-L48

Most of the time to rebind existing prompt toolkit functionality, you need to first find the existing PTK code. I cribbed this from here:
https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/auto_suggest.py#L29-L48

Cool. This also revealed that ctrl-f was default completer. I didn't know that. It is a little easier on my fingers :)

I'm going to close this as answered, but ping here if there are further questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

battaglia01 picture battaglia01  路  3Comments

CJ-Wright picture CJ-Wright  路  6Comments

anki-code picture anki-code  路  4Comments

roxma picture roxma  路  5Comments

74th picture 74th  路  4Comments