Elapsed and execution time display for commands in ZSH
Append this to your ~/.zshrc file.
function preexec() {
timer=$(($(date +%s%0N)/1000000))
}
function precmd() {
if [ $timer ]; then
now=$(($(date +%s%0N)/1000000))
elapsed=$(($now-$timer))
export RPROMPT="%F{cyan}${elapsed}ms %{$reset_color%}"
unset timer
fi
}
Remixed from @adri's snippet.
An easier way to get around this on macOS is to use a prompt expansion format string. (i believe this requires
clock_gettime
and so it's only available on 10.12+). Additionally I can only seem to get millisecond granularity on mac (zsh should be able to go up to%9.
decimal places; in fact zsh aliases%N
to%9.
):It's pure zsh and this also saves you two forks every execution.