Skip to content

Instantly share code, notes, and snippets.

@adam-edison
Created October 5, 2025 22:54
Show Gist options
  • Select an option

  • Save adam-edison/f7ad54b60dbaa0e926837a68e9c3fb96 to your computer and use it in GitHub Desktop.

Select an option

Save adam-edison/f7ad54b60dbaa0e926837a68e9c3fb96 to your computer and use it in GitHub Desktop.
Play a sound on MacOS in zsh terminal after long-running command
# Used for playing sound after a long command is executed
source ~/.zsh/command-sound.zsh
# This is a hook in ZSH that runs before the command execution starts
preexec() {
cmd_start_time=$(date +%s)
}
# This is a hook in ZSH that runs before each command prompt is displayed
precmd() {
if [ -n "$cmd_start_time" ]; then
cmd_end_time=$(date +%s)
elapsed=$((cmd_end_time - cmd_start_time))
if [ $elapsed -gt 5 ]; then # 5 seconds threshold
# Run in background to avoid blocking the terminal
(afplay /System/Library/Sounds/Blow.aiff 2>/dev/null &)
fi
unset cmd_start_time
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment