Triggers in iTerm – Watching For Specific Strings
I was recently performing a git-add in patch mode ( git add -p ) as I normally do, and realized there were a few lines I needed to make sure I didn’t commit. These lines were sandwiched between other lines that I did want to commit, and there were hundreds of changes, so it was hard to spot them. I wondered if iTerm2 had a way of highlighting or alerting when a specific word shows up in the terminal. To my surprise I found a “Triggers” section under the “Profiles » Advanced” tab in the settings for the app. iTerm2 really can do everything!
I configured two triggers to show alerts for the two words that were part of the lines I did not want to commit.
After doing a quick test (typing the word “facebook” in my session popped up an alert immediately), I put it to the test. I stepped through my changes, hitting y for each change I wanted to stage, and n for each change I did not want to stage. Sure enough, when the word “facebook” loaded I got an alert.
The alert behavior is line-by-line. If there were 2 lines containing “facebook”, one after another, in a particular file I got an alert for the first one, I dismissed the alert, then, the next line loaded onto the screen and another alert appeared. I dismissed that one and the remaining lines in the hunk loaded. I then either “edited” the hunk to ignore those two lines, or I used n to reject the whole hunk (when that made sense).
This would have gone unnoticed because there was a lot of content in this particular hunk so these lines would’ve scrolled out of the viewport. Also, they were sandwiched in between lines I did want to stage.
Excluding these deletions from the git add was easy. I hit e to edit the hunk, then remove the “-” prefix on the lines in question and saved. These lines were not staged.
TIP: Alert gets a little annoying if the words are used a lot. You might consider one of the other Actions. Highlight Text is probably fine in most cases. Here’s what Highlight Text looks like by default (the colors are editable):
Red and white aren’t foreign colors to a terminal session, so I’d recommend something that stands out more. Bright purple, blue, yellow, etc. seem to work well.