Add fish-like command abbreviations

This commit is contained in:
David Strobach 2020-03-20 21:46:35 +01:00
parent feeb6315cc
commit d619127503
2 changed files with 38 additions and 0 deletions

View file

@ -1,4 +1,9 @@
{"xontribs": [
{"name": "abbrevs",
"package": "xonsh",
"url": "http://xon.sh",
"description": ["Command abbreviations."]
},
{"name": "apt_tabcomplete",
"package": "xonsh-apt-tabcomplete",
"url": "https://github.com/DangerOnTheRanger/xonsh-apt-tabcomplete",

33
xontrib/abbrevs.py Normal file
View file

@ -0,0 +1,33 @@
"""Command abbreviations."""
import builtins
from xonsh.platform import ptk_shell_type
__all__ = ()
builtins.__xonsh__.ctx["abbrevs"] = dict()
def expand_abbrev(buffer):
word = buffer.document.get_word_before_cursor()
if "abbrevs" not in builtins.__xonsh__.ctx.keys():
return
abbrevs = builtins.__xonsh__.ctx["abbrevs"]
if word in abbrevs.keys():
buffer.delete_before_cursor(count=len(word))
buffer.insert_text(abbrevs[word])
@events.on_ptk_create
def custom_keybindings(bindings, **kw):
if ptk_shell_type() == "prompt_toolkit2":
handler = bindings.add
else:
handler = bindings.registry.add_binding
@handler(" ")
def space(event):
buffer = event.app.current_buffer
expand_abbrev(buffer)
buffer.insert_text(" ")