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

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:

[alias]
    ack = -c color.grep.linenumber=\"bold yellow\" \
          -c color.grep.filename=\"bold green\" \
          -c color.grep.match=\"reverse yellow\" \
          grep --break --heading --line-number

(this is expressed as you'd see it in git config --global --edit since the quoting is messy).

Or, to set it up in one command:

git config --global alias.ack '-c color.grep.linenumber="bold yellow"
    -c color.grep.filename="bold green"
    -c color.grep.match="reverse yellow"
    grep --break --heading --line-number'

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 ack alias.


From Travis Jeffery, to group git grep output like ack:

git config --global alias.g "grep --break --heading --line-number"

And then use git g like you would git grep:

git g <search_string>

This is not a complete match to ack output -- it's missing the color highlighting -- but for a quick solution it's ok.

Tags:

Git

Grep

Ack