Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

grep

ack

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 ack (ack-grep in Ubuntu), one thing left to be desired is the output formatting, which is unfortunately much more like grep than ack. Go figure.

ack:

  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 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 ack output?

like image 879
CivFan Avatar asked Sep 19 '16 20:09

CivFan


People also ask

How do you print only the matched pattern using grep?

Displaying only the matched pattern : By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option. 6. Show line number while displaying the output using grep -n : To show the line number of file with the line matched.

How does git grep work?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.

Which statement is the best comparison between git grep and grep?

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.

Which command is used to print the given pattern in git?

grep command in details grep searches standard input when no files were given or specified files/directories for occurrences of the pattern . When grep finds a line that contains the pattern it prints it to the standard output.


2 Answers

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.

like image 177
torek Avatar answered Oct 23 '22 07:10

torek


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.

like image 5
CivFan Avatar answered Oct 23 '22 06:10

CivFan