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のプロパティを見れば、大抵のことは設定できそう。
- https://python-prompt-toolkit.readthedocs.io/en/master/pages/reference.html#module-prompt_toolkit.buffer
- https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/buffer.py
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 |
+------------------+----------------------+