Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I run grep -ir "somethingtomatch" . from the current directory, I typically get results like this:

./some/path/file1.html: filecontent filecontent keyword filecontent
./some/path/file2.html: filecontent filecontent filecontent keyword
./some/path/file3.html: filecontent keyword filecontent filecontent
./some/path/file4.html: keyword filecontent filecontent filecontent

I used grep --color=auto -ir 'somethingtomatch" . but it only highlights the keywords in white on a red highlight. I'm trying to get file names on the left color-coded too. How do I do that?

I'm using in OS X with bash and xterm (and I tried xterm-color too).

share|improve this question

You can change the colors with the $GREP_COLORS environment variable. In your case, you could try to export GREP_COLORS='fn=1;32' to change the color of the filename to green.

share|improve this answer
Doesn't seem to be working. Here's what I tried: export TERM=xterm-color export GREP_COLORS='fn=1;32' grep --color=auto -ir 'keyword' . – chimerical Mar 20 '11 at 22:05
Ok, it seems like you're suffering under "". I don't know much about it, but maybe this link can help you. – p.vitzliputzli Mar 21 '11 at 18:40

You would have to postprocess the output. Something like this might work:

$ grep --color -ir 'pattern' files | perl -pe 's/^([^:]+):/chomp(my $f = `ls --color \Q$1`); $f/e'

(I'm assuming you're displaying output from a Linux system, since OSX ls doesn't colorize files usefully for this.)

(NB: the distinction between this answer and the $GREP_COLORS one is that the latter uses a fixed color, whereas mine queries ls --color.)

share|improve this answer
OSX can colorize files using ls -G – kyle k Apr 19 '14 at 1:08

You can use ack, which allows you to set environment variables for filename, line number and the match itself.

share|improve this answer

You must log in to answer this question.

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