Often I want to know how long it took for a particular command to finish.

An obvious solution to use the time(1) command does not work without a degree of anticipation on my part that I do not normally posess.

At some point I became sufficiently annoyed to actually add some hooks to my .zshrc. All commands executed in an iteractive shell are timed, but the reporting is done only for those that took longer than 10 seconds to execute.

This ugly code does the job:

note_remind=0
note_ignore="yes"
note_command="?"

note_report()
{
    echo ""
    echo "note_report: $note_command completed in $1 seconds"
}

preexec()
{
    if [ "x$TTY" != "x" ]; then
        note_remind="$SECONDS"
        note_ignore=""
        note_command="$2"
    fi
}

precmd()
{
    local xx
    if [ "x$TTY" != "x" ]; then
        if [ "x$note_ignore" = "x" ]; then
            note_ignore="yes"
            xx=$(($SECONDS-$note_remind))
            if [ $xx -gt 10 ]; then
                if [ $TTYIDLE -gt 10 ]; then
                    note_report $xx
                fi
            fi
        fi
    fi
}

Enjoy.

comment 1
Thanks. this is just what I was looking for!
Comment by adlr Mon Jun 22 08:24:29 2009
comment 2
What about export REPORTTIME=10 ?
Comment by Gyom Tue Jun 30 08:41:50 2009
comment 3

Gyom,

Cool, I did not know about REPORTTIME.

That said, if what one is interested in is the wall clock, REPORTTIME would not do the trick. But for many cases when time reporting is actually useful, REPORTTIME is better than my horrible hack.

Comment by tobez Tue Jun 30 11:38:07 2009