Help us understand the problem. What is going on with this article?

xonsh[ptk]で、Suggestionを確定するキーバインドを設定する

More than 1 year has passed since last update.

ptkのフラグを付けてインストールしたxonshにおいて、Auto Suggestionのキーバインドを追加する。

pip3 install xonsh[ptk]

xonsh で、Auto Suggestionを確定するキーはデフォルトでは右矢印である(他にもあるかも)。

これを、Ctrl-kに設定する場合、.xonshrc に以下のように追加する。

from prompt_toolkit.keys import Keys
@events.on_ptk_create
def custom_keybindings(bindings, **kw):
    handler = bindings.add

    @handler(Keys.ControlK)
    def select_suggestion(event):
        if event.current_buffer.suggestion:
            event.current_buffer.insert_text(event.current_buffer.suggestion.text)

eventのcurrent_bufferのプロパティを見れば、大抵のことは設定できそう。

Vimモードでinsertの場合のみ、適用するには以下の通り。

from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import Condition, ViInsertMode
@events.on_ptk_create
def custom_keybindings(bindings, **kw):
    handler = bindings.add
    insert_mode = ViInsertMode()

    @handler(Keys.ControlK, filter=insert_mode)
    def select_suggestion(event):
        if event.current_buffer.suggestion:
            event.current_buffer.insert_text(event.current_buffer.suggestion.text)

なお、環境は以下の通り。

+------------------+----------------------+
| xonsh            | 0.7.10               |
| Git SHA          | fe44f4a9             |
| Commit Date      | Sep 26 12:14:51 2018 |
| Python           | 3.7.0                |
| PLY              | 3.9                  |
| have readline    | True                 |
| prompt toolkit   | 2.0.4                |
| shell type       | prompt_toolkit2      |
| pygments         | None                 |
| on posix         | True                 |
| on linux         | False                |
| on darwin        | True                 |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
+------------------+----------------------+
74th
k8sとPythonとVSCodeでごはん食べてる。 github: @74th twitter: @74th
https://github.com/74th
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away