Apr 05,
2016

Zsh fancy prompts

The Z shell, usually called zsh, is a command interpreter with a big legacy and packed with features. The documentation is definitely ponderous and I'm always surprised to find little known features.

I have a little suggestion for the people that like me use to put in their prompt an indicator with the battery level, adding a couple of lines will keep the values always updated instead of only when executing a command:

# update prompt every 60 sec
function update-prompt-and-resched() {
    # find existing scheduled event and remove it
    integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)update-prompt-and-resched]}
    (( i )) && sched -$i
    sched +60 update-prompt-and-resched
    zle && zle reset-prompt
    return 0
}

To avoid terminal redraws when I'm looking at the scrollback buffer and the prompt is updated by the scheduler, I disabled the option "Scroll on output" in my terminal.

All the open shells now have the correct battery value, otherwise one can use Tmux and put the indicators in the status line.

zsh