Highlight selected words in terminal output with grep

A one-line shell function to visually emphasize search terms in the terminal output of a program

by on

Don’t miss the haystack for the needles

Let’s say you have a program or script that you run at the terminal, and it prints out several pages worth of text. This executable was written ad hoc, and so lacks a built-in way to tune or filter the output.

Among the several hundred lines it outputs on every run, there is a chance that one or two lines might appear that are relevant and actionable for you. In the case that those lines appear, you also need the surrounding lines to give you context about the actions you’re going to take.

Here are some of the situations where I’ve encountered programs with these characteristics:

  • spinning up a set of virtual machines in a local environment
  • processing files in a directory
  • scraping a set of websites
  • updating rows in a remote database
  • running a noisy and broken suite of tests
  • or any combination of the above

Existing Solutions

The easiest solution is to use the Find feature of your terminal, if it supports it. The GNOME terminal usually has this set up with the key-binding of Ctrl + Shift + F. On Macs, I’ve heard this is bound to Command + F. This is great, and most of the time it’s exactly what you’ll need.

Sometimes, though, jumping between results can be disconcerting. Furthermore, if you’re repeatedly running the same command in the same terminal, your search function might page you back to previous invocations, which is unhelpful.

You can also pipe the output of your command to less with

! command | less

You can then use the less keyboard commands#Frequentlyusedcommands) to search, scroll and browse through the output.

In between these two options, there’s another solution I found which was quick, easy and fit my purposes. I thought I’d share.

A shell function to highlight results

Add the following shell function to your .bashrc file 1.

highlight() { grep --color "$1\|\$"; }

You can then invoke it using

! <command> [args...] | highlight [search-term]

This uses grep to color the specific words in your search term, and so make it visible to you as you scroll through the output.

For some use cases, I find this is more helpful than using less or the find function in my terminal. Maybe you’ll find it useful too.

  1. The .bashrc file is run on the setup of every new interactive shell, and so it’s a good place to put utility commands like this which increase your usability of the shell. ↩︎
This article was filed under:
Terminal Tricks