Linux, plaintext & minimalism

Notice: This blog have moved to hunden.linuxkompis.se. Read more about it here: My blog have a new home and a new address.

April 9, 2019

Display the time it took to run a command in zsh

zsh-command-time is a neat little plugin for zsh that displays the time it took to run a command.

Here’s a demo:

$ sleep 5 ; echo "Every 60 seconds a minute passes in Africa."
Every 60 seconds a minute passes in Africa.

[ Time: 5s ]


Download the plugin via git to your preferable folder of choice:

$ git clone https://github.com/popstas/zsh-command-time

Add this line to your ~/.zshrc configuration file (and make sure that the path points to your file):

source "$HOME/.zsh/plugins/zsh-command-time/command-time.plugin.zsh"

This part is also going into your ~/.zshrc configuration file:

# Different output for zsh-command-time
zsh_command_time() {
    if [ -n "$ZSH_COMMAND_TIME" ]; then
        if [ "$ZSH_COMMAND_TIME" -le 60 ]; then
            timer_show="$fg[green]${ZSH_COMMAND_TIME}s $fg[default]"
        elif [ "$ZSH_COMMAND_TIME" -gt 60 ] && [ "$ZSH_COMMAND_TIME" -le 180 ]; then
            timer_show="$fg[yellow]${min}m ${sec}s $fg[default]"
            if [ "$hours" -gt 0 ]; then
                timer_show="$fg[red]${hours}h ${min}m ${sec}s $fg[default]"
                timer_show="$fg[red]${min}m ${sec}s"
        printf "\n[ ${ZSH_COMMAND_TIME_MSG}\n" "$timer_show]"

To prevent too much clutter in the main configuration file I added the above part to a file called ~/.zsh/functions.zsh. If you wish to do the same thing, don’t forget to source the file in your ~/.zshrc configuration file as well:

source "$HOME/.zsh/functions.zsh"

Source your configuration with $ source ~/.zshrc (or just restart the terminal emulator) when you’re done for the changes to take effect.

Tags: Zsh | Shell | Utils


I don't have a commenting system simply because I haven't found a solution that I like. If you want to leave any feedback and/or have any questions, feel free to contact me either via e-mail, fediverse, XMPP or IRC (I'm Hund at freenode.net).