LoginSignup
0
0

More than 5 years have passed since last update.

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

Posted at

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      |
+------------------+----------------------+
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0