Skip to content

Instantly share code, notes, and snippets.

@knadh
Last active April 20, 2024 06:35
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save knadh/123bca5cfdae8645db750bfb49cb44b0 to your computer and use it in GitHub Desktop.
Save knadh/123bca5cfdae8645db750bfb49cb44b0 to your computer and use it in GitHub Desktop.
Elapsed and execution time for commands in ZSH

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.

@akhildevelops
Copy link

Shows execution time at the end of the prompt

OG_PROMPT=$PROMPT
function preexec() {
  timer=$(($(date +%s%0N)/1000000))
}

function precmd() {
  if [ $timer ]; then
    now=$(($(date +%s%0N)/1000000))
    elapsed=$(($now-$timer))

    export PROMPT="$OG_PROMPT%F{cyan}${elapsed}ms %{$reset_color%}> "
    unset timer
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment