Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Created February 26, 2025 21:02
Show Gist options
  • Save mattmc3/a1e3e2d4d311751c02f9317bd01c958a to your computer and use it in GitHub Desktop.
Save mattmc3/a1e3e2d4d311751c02f9317bd01c958a to your computer and use it in GitHub Desktop.
Xonsh magic enter
from xonsh.built_ins import XSH
$MAGIC_ENTER_GIT_COMMAND = "git status -sb ."
$MAGIC_ENTER_OTHER_COMMAND = "ls -lahF ."
$MAGIC_ENTER_RUN_COMMAND = ""
@events.on_transform_command
def magic_enter(cmd, **_):
"""Custom Enter key behavior: runs 'ls' if no command was given."""
if not cmd.strip():
if p'$PWD/.git'.exists():
$MAGIC_ENTER_RUN_COMMAND = $MAGIC_ENTER_GIT_COMMAND
else:
$MAGIC_ENTER_RUN_COMMAND = $MAGIC_ENTER_OTHER_COMMAND
return cmd
@events.on_pre_prompt
def run_magic_enter():
if $MAGIC_ENTER_RUN_COMMAND != "":
$[@($MAGIC_ENTER_RUN_COMMAND.split())]
$MAGIC_ENTER_RUN_COMMAND = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment