CivFan CivFan - 1 year ago 80
Git Question

How do you make `git grep` output look like `ack` output?

I've recently found

git grep
and come to like its speed and de facto searching of only the files in the repo. But coming from
in Ubuntu), one thing left to be desired is the output formatting, which is unfortunately much more like
(go figure).


  1. Prints the matching filename on the first line by itself.

  2. Color highlights the matching filename a bold green.

  3. Prints the line number, and only the line number, with each matching line.

  4. Color highlights the line number a bold yellow.

  5. Color highlights each matching string a background bold yellow.

  6. Prints a blank line between matches from different files.

On the other hand,
git grep

  • Prints the filename on every matching line.

  • Prints no line number.

  • Prints no blank line between matches from different files.

  • Color highlights only the matching text, a bold red.

Is there any set of
git grep
options, or combo with other tools, that can make
git grep
output look like

Answer Source

You've already answered part of your own question (--break inserts a blank line between files, --heading prints the file name separately, and -n or --line-number gives you line numbers on each line).

The rest is just color options, which are set in git config via the color.grep.<slot> entries. See the documentation for full details, but note that based on what you asked for, I think this does the trick:

    g = -c color.grep.linenumber=\"bold yellow\" \
            -c color.grep.match=\"reverse bold yellow\" \
            grep --break --heading --line-number

(this is expressed as you'd see it in git config --global --edit since the quoting is messy). Add or subtract -c options to change whatever colors you like, and/or set them to your preferred defaults by setting color.grep.<name> = color instead of using the git g alias.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download