iTerm2 Tips & Tricks
Notifications
When you start a command in iTerm2 and you want to know when it finishes, there are a handful of tricks you can use. I only show the core concepts here, but if you would like some ideas on how to incorporate them into your workflow, you can read this blog post: Useful notifications in iTerm2.
Method 1: “say” command
There is a command built in to macOS that allows you to have a string read aloud by the system voice: say
. To use this to get a notification when your command finishes, just append it to the command you are running. This can also be used from other languages, by invoking a subprocess.
Advantages:
- Easy, no installation or configuration
Disadvantages:
- Notification is sound-only
- Does not work over SSH
For a bit more personalization, you can also use afplay
to play any audio file on your system.
Method 2: Use iTerm2 features
iTerm2 supports several proprietary escape codes, including some that allow you to send custom notifications.
Advantages:
- Works even over SSH
- Can be combined with previous to get audio/visual notifications
Disadvantages:
- Requires adding some short functions or memorizing the escape code
Here are the escape codes as functions that you can copy/paste:
The iterm_notify
function sends a system notification with the provided text.
The iterm_bounce
function causes the Dock icon for iTerm2 to, well, bounce.
Finally, the iterm_badge
function adds a text label to the background of your terminal. This is useful for labeling tabs when you have many of them open.
Method 3: Shell integration
If you use iTerm2’s shell integration, then you can use a single keyboard shortcut to get a notification when the command finishes.
Advantages:
- Activates on-demand with a keystroke
- Works even over SSH
Disadvantages:
- Notification text cannot be customized
- Requires installation on each system where you want to use it
Once you have installed the shell integration, you can activate it by using Edit ▸ Marks and Annotations ▸ Alerts ▸ Alert on Next Mark, or by pressing keyboard shortcut ⌥ ⌘ A
(Option-Command-A). The resulting notification looks like this: