Get desktop notifications when terminal commands finish

Do you often run commands like terraform plan that take minutes (or hours) to finish? Then your days are probably full of watching endless terminal outputs or switching apps back and forth. Has it finished yet? Oh no still going.

Here's how you can fix your Mac OS setup to deal with it:

The solution is based on the great terminal-notifier utility. But if you use tmux (you should), there's a few things you need to set up to make it perfect.

Add a function to your .profile, something like:

    tnotify() {
    	"$@" && (echo "Finished $@" | terminal-notifier -sound default)

Set an option in your .tmux.conf to allow events propagation from tmux:

    set-option -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL"

Finally, combine the tnotify function with your bash aliases:

    alias tfp='tnotify terraform plan -out my.tfplan'

source your .profile and enjoy the magic