DEV Community

Cover image for Highlight Console Warnings and Errors in iTerm2
christa
christa

Posted on

Highlight Console Warnings and Errors in iTerm2

The Issue

Have you ever had the issue of scrolling through long blocks of output in your terminal and miss errors and warnings entirely. This kept happening to me frequently until I finally had enough and took it to Google.

iTerm2 has a text highlighting feature that offers the solution needed to stop missing those errors.

The Solution

Highlight Text offers a wide variety of features and abilities, but today we're going to utilize it to use regular expressions to check for different kind of messaging.

Enable Highlighting

In your iTerm2 Profile settings (iTerm2 Preferences => Profiles) switch to Advanced and look for the Triggers section. Click Edit to add your own triggers. Select the '+' symbol at the bottom left to create a new one.

Add the following regular expression to react on lines containing the text "error":



(?i:.*(error|fatal).*)


Enter fullscreen mode Exit fullscreen mode

Flags are placed at the start of the regex. We use the case insensitive flag i so the regex starts with ?i:. After the colon, the regular expression begins.

Select Highlight Text as an action and customize the text color and background as needed. You can either create multiple triggers or define multiple words in your pattern to highlight.

Create a highlight for warnings:



(?i:.*warn|warning.*)


Enter fullscreen mode Exit fullscreen mode

To learn more about the regular expressions, check out regex101 to get more details.

When you are done, your triggers may look something like this:

Defined triggers in iTerm2

And the output:

iTerm2 window with console log messages: console log (this is a regular console log) in plain text, console warning (this is a warning) in yellow text, and console error (this is an error) in red text

Top comments (0)