Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 Terminal.app in OS X with bash and xterm (and I tried xterm-color too).

share|improve this question

3 Answers 3

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 "Terminal.app". 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 at 1:08

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

http://betterthangrep.com/

share|improve this answer

Your Answer

 
discard

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.