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

I recently found git grep and began to love its speed and the de facto search for only files in the repo. But, based on ack ( ack-grep in Ubuntu), you need to leave the desired output formatting, which, unfortunately, is much more like grep than ack . Hover over your mouse.

ack

  • Matching the file name itself is the first line .
  • Color highlights the matching file name a bold green .
  • Prints line number and only line number . each corresponding line ..>
  • Color highlights line number a bold yellow .
  • Color highlights each matching line a background is yellow .
  • Prints a blank line between matches from different files.

git grep , on the other hand:

  • Prints the file name on each matching line .
  • Print line number .
  • Print without blank lines between matches from different files.
  • Color highlights text only bold red .

Is there any set of git grep options or a combination with other tools that can make git grep output look like ack output?

+5
source share
2 answers

You already answered a part of your question ( --break inserts an empty line between files, --heading prints the file name separately, and -n or --line-number gives line numbers in each line).

The rest are just color options that are set in git config via the color.grep.<slot> entries. See the documentation for more details, but note that depending on what you requested, 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 see it in git config --global --edit , as quoting is messy).

Or, to configure it with 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 any colors you like, and / or set them to your default preference by setting color.grep.<name> = color instead of using the git ack alias.

+9
source

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 , as you would git grep :

 git g <search_string> 

This is not a complete match with the ack output - it skips color highlighting, but for a quick fix, that's fine.

+3
source

All Articles