beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 138 forks source link

Add docs on how to make grep look like ack #619

Closed petdance closed 7 years ago

petdance commented 7 years ago

I think a lot of people use ack strictly because of the grouping and the pretty colors. It's trivial to make grep do this and then you don't have to use ack.

Put something in the ack docs that explains how.

packy commented 7 years ago

I'm just commenting in here as I find things:

I know this doesn't do the grouping, but I think I'll need to do a bash function for that.

And here's the function:

mygrep () {
  GREP_COLOR=${MYGREP_COLOR:-'30;43'} \
  /usr/bin/grep -RH --line-number --color=always $* | \
    MYGREP_NUMBER_COLOR=${MYGREP_NUMBER_COLOR:-'33;40'}  \
    MYGREP_FILE_COLOR=${MYGREP_FILE_COLOR:-'31;40'} \
    perl -ne 'BEGIN { $lastfile = q{}; }
            ($file, $line, $match) = split ":", $_, 3;
            if ($file ne $lastfile) {
                print "\n" if $lastfile ne q{};
                print "\e[".$ENV{MYGREP_FILE_COLOR}."m$file\e[0m\n";
                $lastfile = $file;
            }
            print "\e[".$ENV{MYGREP_NUMBER_COLOR}."m$line\e[0m:$match";
    ' | less -FRX
}

It takes the output of grep with recursion, line numbers and file names always on, and uses a perl almost one-liner to print the filenames in color before the results of the match. The colors default to what I've got ack defaulted to use, but they can be controlled through the MYGREP_COLOR (the color of the matches), MYGREP_FILE_COLOR (the color of the filenames) and MYGREP_NUMBER_COLOR (the color of the line numbers) variables.

n1vux commented 7 years ago

GNU doesn't want to make it easy. While yes one could export GREP_OPTIONS=-R but they'll warn because that is deprecated as non-portable. sigh https://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html#Environment-Variables ​They say " Please use an alias or script instead", which means to make grep work differently you have to rename the new behavior.

petdance commented 7 years ago

This issue was moved to petdance/ack3#30