Xonsh the shell for XXIth century

Linux shell xonsh bash sh csh tcsh linux lazy

Written on March 1, 2020

I am a happy user of xonsh shell, and let sh, csh, tcsh and bash behind…

Xonsh merges python and shell, with a terrific prompt!

Discovering unix last century (hp-ux), i quickly enhanced my shell to tcsh. that included tab-completion and nice scripts! such a marvel compared to sh or csh.

Then came my first encounter with bash in linux: not a good memory. so i used to install tcsh on my box. except when i could not…

Alas, always fighting to install a shell, i finally embraced the default bash. that lasted probably 20 years.

During those 20 years, things evolved (tab, color, hints, …) yet, the overall state did not change.

A new era of shell is coming. enter fish and xonsh (and more).

  • before: 90% of my use of prompt: cd, ls, bc, cat, launch…
  • after: same, but all in python (no more bc)

xonsh

from xon.sh

xonsh is a python-powered, cross-platform, unix-gazing shell language and command prompt. the language is a superset of python 3.5+ with additional shell primitives that you are used to from bash and ipython. it works on all major systems including linux, mac osx, and windows. xonsh is meant for the daily use of experts and novices alike.

positive slightly negative
Python from the console slightly slow to start (on my old box)
fantastic play with prompt (see below) syntax for looping on files

Xonsh prompt for the pleasure

Xonsh - start/stop

Details of .xonshrc

.xonshrc - Python code

# Update PROMPT with current time (every second)
import datetime
$MULTILINE_PROMPT = ''
$PROMPT_REFRESH_INTERVAL = 1
$UPDATE_PROMPT_ON_KEYPRESS = True
# SEEMS Very slow when "{branch_color}{curr_branch: {}}" in PROMPT...

DEFAULT_PROMPT = '\n{GREEN}β”Œβ”€[%s] {RED}[{user}@{hostname}]' \
               + ' {BOLD_CYAN}[{cwd}]{NO_COLOR}\n{GREEN}└─>{NO_COLOR} '
def update_prompt():
    return(DEFAULT_PROMPT % datetime.datetime.now().strftime("%H:%M:%S"))
$PROMPT = update_prompt

# After command execution:
# - Update RIGHT_PROMPT with command duration (and more)
# - emphasize on error if RC != 0
@events.on_postcommand
def display_command_status(cmd, rtn, out, ts):
    duration = ts[1] - ts[0]

    if duration > 60:
        $RIGHT_PROMPT = "\nπŸ•™ %s " % str(datetime.timedelta(seconds=int(duration)))
    elif duration > 0.2:
        $RIGHT_PROMPT = "\nπŸ•™ %.2fs " % duration
    else:
        $RIGHT_PROMPT = "\n<Ο‡> "

    if rtn != 0:
        $RIGHT_PROMPT = $RIGHT_PROMPT + "\n⚠ " + str(rtn) + " ⚠"

        print("\n" + "⚠" * 25)
        print("⚠ " + str(rtn))
        if cmd[len(cmd)-1] == '\n':
            cmd_p = cmd[0:len(cmd)-1]
        else:
            cmd_p = cmd
        print("⚠ " + cmd_p)
        print("⚠" * 25 + "\n")

# Clean RIGHT_PROMPT when no command executed (Empty line, Enter/Enter)
$RIGHT_PROMPT = "Ο‡ "

@events.on_post_prompt
def display_post_prompt():
    $RIGHT_PROMPT = "Ο‡"


β—€ Bluetooth Low Energy everywhere! β–  β–  β–  ArchLinux: pacman hook (to see the description) β–Ά


In the same category: