Constantly updated clock in zsh prompt?

anon picture anon · Feb 2, 2010 · Viewed 7k times · Source

I know that I can exec a date command in my zsh prompt. However, it shows the old time; to see the current time, I have to hit <return> and get a new prompt with the current time.

Is there a way to configure the zsh prompt to constantly update itself every second?

Answer

nitarshan picture nitarshan · Jul 29, 2013

Note: I wrote this answer for a similar question, but seeing how this question has more views I think reposting my answer here would be useful.

This is in fact possible without resorting to strange hacks. I've got this in my .zshrc

RPROMPT='[%D{%L:%M:%S %p}]'

TMOUT=1

TRAPALRM() {
    zle reset-prompt
}

The TRAPALRM function gets called every TMOUT seconds (in this case 1), and here it performs a prompt refresh, and does so until a command starts execution (and it doesn't interfere with anything you type on the prompt before hitting enter).

Source: http://www.zsh.org/mla/users/2007/msg00944.html (It's from 2007!)