Command duration in fish shell

If you’re interested in how long your commands run you might want to display the duration of the last command in the right prompt. I’ve made a similar blog post for ZSH in 2013 but I’m now using the fish shell.

To make it work create the file ~/.config/fish/functions/ with this content:

function fish_right_prompt
    if test $CMD_DURATION
        # Show duration of the last command in seconds
        set duration (echo "$CMD_DURATION 1000" | awk '{printf "%.3fs", $1 / $2}')
        echo $duration
    echo "😄"

And then source it in your ~/.config/fish/

. ~/.config/fish/functions/

Update: Adding OS X notifications

Sometimes it is nice to be notified when a long running command (> 10 sec) finishes. But because displaying notifications all the time can get quite annoying I’ve limited the notifications to be only shown for some commands and when iTerm is not in focus.

function fish_right_prompt
    if test $CMD_DURATION
        # Show duration of the last command
        set duration (echo "$CMD_DURATION 1000" | awk '{printf "%.3fs", $1 / $2}')
        echo $duration

        # OS X notification when a command takes longer than notify_duration
        set notify_duration 10000
        set exclude_cmd "bash|less|man|more|ssh"
        if begin
                test $CMD_DURATION -gt $notify_duration
                and echo $history[1] | grep -vqE "^($exclude_cmd).*"

            # Only show the notification if iTerm is not focused
            echo "
                tell application \"System Events\"
                    set activeApp to name of first application process whose frontmost is true
                    if \"iTerm\" is not in activeApp then
                        display notification \"Finished in $duration\" with title \"$history[1]\"
                    end if
                end tell
                " | osascript
    # Be nice :)
    echo "😄"

Related Posts

Learning Elixir: 1 year later

One year ago I dived into learning Elixir. It's time for a retrospective.

Elixir side-project #2: A Graphql API with Elixir (2/2)

Building a GraphQL API for my recipe planning side-project.

Elixir side-project #2: Planning Recipes with a Supermarket API (1/2)

Introduction to my next side-project to learn Elixir.

Learning Elixir: My side-project

Learnings from starting on a side-project written in Elixir.

Learning How to Learn – Online Course Notes

My notes for the online course “Learning How to Learn”.

Why the Elixir language has great potential

Elixir enables creating fast, reliable and concurrent modern applications with high productivity

Tips for making screencasts for presentations

As an alternative to live demos you can play screen casts during your tech talk.

Trust and Integrity

My notes for the Speed of Trust by FranklinCovey.

NodeJS: Factory Function Pattern, Dependency Registry and Hot Reloading

This article shows how to combine Factory Functions with a rapid feedback loop using hot reloading.

Better back swiping with the React Native Navigator

Swiping back works only close to the edge by default which can be hard to accomplish. It is easy to optimize this.