18

I have seen on some Linux, that grep is configured to highlight the match, and print the matching file.

How can I configure grep to highlight matches?

6 Answers 6

19

I've found that the best way to pimp grep is to use ack, which is essentially recursive grep with an intelligent ignore list (e.g., doesn't search .svn directories, ignores backup files, etc.), colour highlighting of results and perl regexps. It's what you want grep to do 98.6% of the time.

4
  • 5
    +1 for mentioning ack. Don't forget to include the --pager="less -R" option to maintain maintain highlighting for paginated results. Might also be worth mentioning that on Ubuntu, it is referred to as "ack-grep" since "ack" is taken.
    – Shawn Chin
    Aug 11, 2010 at 13:19
  • I use ack as well, but sometime you don't have it available..
    – elmarco
    Aug 13, 2010 at 11:06
  • 2
    Since ack is just a perl script, you can make it available anywhere you have perl installed, which isn't a high bar.
    – Kai
    Aug 17, 2010 at 20:37
  • Ack is good for it's out-of-the-box defaults, but in the time it takes you to install ack, you could have configured some defaults for grep - see my answer elsewhere on this page. I did this once in the 1980s, and since then grep has done everything ack does. (only grep is actually faster, despite ack's loud claims to the contrary) Oct 4, 2011 at 11:16
11
alias cgrep='grep -nR --color'

Usage:

$ cgrep somestring /path/to/dir/or/file/with*/possible/*wild.card

Also one of my favorites:

$ pgrep some-hanging-process

will list all pids of processes that match the name of some-hanging-process which you can use in following situation:

$ kill $(pgrep some-hanging-process)
2
  • 12
    there is pkill command for the last one
    – diega
    Aug 10, 2010 at 23:34
  • 1
    With recent versions of GNU grep, grep -r should generally be preferred over grep -R as the former does not traverse symlinks. Aug 18, 2014 at 18:57
7

I set this in my .bashrc , instead of redefining grep using an alias:

    export GREP_OPTIONS="--color=auto"

For me, this works on Linux, MacOSX & FreeBSD.

4
  • GREP_OPTIONS should be considered harmfull. I recommend to use alias instead. See here: bugs.launchpad.net/ubuntu/+bug/67141
    – Lesmana
    Mar 25, 2011 at 16:44
  • I agree that GREP_OPTIONS can be a problem in certain scripts. But as I argued in bug 67141, GREP_OPTIONS allows the user set one option for many forms of grep (grep, egrep, fgrep, rgrep, zgrep, bzgrep, bzfgrep, bzegrep, etc.), whereas an alias will need to be set for every form of *grep, and there are many types of grep. Mar 25, 2011 at 18:56
  • I used to use GREP_OPTIONS for years (nay, decades) but the harm it causes is very real. Any tools which invoke grep with this env var set will inherit your modified default behaviour, and this WILL break those tools. Avoid this problem entirely by defining an alias or an executable wrapper script instead. Oct 4, 2011 at 11:18
  • Scripts must sanitize their environment. No ifs, buts, or howevers allowed. Period. (Yes, it is a royal pain in the lower back...).
    – vonbrand
    Mar 15, 2013 at 15:02
3

I'd recommend avoiding the environment variable GREP_OPTIONS, it will affect every invocation of grep, even those embedded within other tools. If those tools expect grep to behave one way, and you change that behaviour, it will and does break those tools.

Instead, you can create an alias, which works well. This will only affect calls to grep from your interactive shell (i.e. that you type yourself).

The final option, that I like most of all, is to create a wrapper script that invokes grep. I prefer this over an alias, because I can invoke this wrapper from other programs. E.g. in vim by setting vimgrep, so that my searches from within vim behave identically to searches at the command-line.

$ cat `which grp`
#!/usr/bin/env bash
grep -rI --color --exclude-dir=\.bzr --exclude-dir=\.git --exclude-dir=\.hg --exclude-dir=\.svn --exclude-dir=build --exclude-dir=dist --exclude=tags $*

Invoke this using:

$ grp pattern dir

e.g.

$ grp pattern .

will search for instances of 'pattern' in all text files in the current dir and subdirs.

Notice that I call my script 'grp', rather than shadowing 'grep', so that I'm always aware of whether I'm invoking grep with my customised defaults or not.

By default I include:

-r : search subdirs recursive
-I : skip binary files
--color : highlight matches in color
--exclude-dir : skip specified directories and their subdirs
--exclude : skip specified files

I think everyone would want to skip source control dirs: .hg .git .bzr .svn

Skipping 'build' and 'dist' are Python-isms, and probably don't apply to most people. No doubt you'll develop your own idiosyncrasies as you work.

'tags' is the output of ctags, that I use for 'go to function definition' and the like in tools like vim. As such it contains at least one copy of every word and symbol from my source code, so it's worth skipping from your search results.

"$*" at the end is the bash syntax for "and all the other params from the command-line", so you can pass in the pattern and dir to search as normal, and override any other command line flags you'd like.

2

The --color option has been already mentioned several times, but I'd like to add that it's possible to configure the color in which the matches will be highlighted using an environment variable

export GREP_COLOR="1;33"

The color should be encoded using ANSI color codes, for reference

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37
1
  • 1
    GREP_COLOR is deprecated. Use GREP_COLORS instead.
    – user3356
    Apr 6, 2012 at 21:48
2

Since I didn't see any examples of actually setting colors, here is a simple setup for GNU grep:

# turn on colors, natch.
export GREP_OPTIONS="--color=auto"

if [[ $(echotc Co) -ge 256 ]]; then
  # 256 color terminals
  export GREP_COLORS="mt=38;5;118:sl=:cx=:fn=38;5;18:ln=1;30:bn=37:se=30"
else
  # everybody else
  export GREP_COLORS="mt=31:sl=:cx=:fn=34:ln=1;30:bn=30:se=30"
fi

Be careful about GREP_OPTIONS; don't use it for anything other than things like --color=auto or it'll mess up any scripts that use grep on your system.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .