Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

up vote 9 down vote accepted
alias cgrep='grep -nR --color'


$ 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)
share|improve this answer
there is pkill command for the last one –  diega Aug 10 '10 at 23:34
With recent versions of GNU grep, grep -r should generally be preferred over grep -R as the former does not traverse symlinks. –  Stéphane Chazelas yesterday

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.

share|improve this answer
+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 '10 at 13:19
I use ack as well, but sometime you don't have it available.. –  elmarco Aug 13 '10 at 11:06
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 '10 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) –  Jonathan Hartley Oct 4 '11 at 11:16

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.

share|improve this answer
GREP_OPTIONS should be considered harmfull. I recommend to use alias instead. See here: bugs.launchpad.net/ubuntu/+bug/67141 –  lesmana Mar 25 '11 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. –  Stefan Lasiewski Mar 25 '11 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. –  Jonathan Hartley Oct 4 '11 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 '13 at 15:02

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
share|improve this answer
GREP_COLOR is deprecated. Use GREP_COLORS instead. –  g33kz0r Apr 6 '12 at 21:48

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


$ 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.

share|improve this answer

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"
  # everybody else
  export GREP_COLORS="mt=31:sl=:cx=:fn=34:ln=1;30:bn=30:se=30"

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.