38

This question specifies that -P allows GNU grep to grep for a tab

grep -P '\t' config/file.txt

However, using git grep, I can't work out how to grep for a tab:

git grep '\t' # Looks for files with the letter "t" instead
git grep -P '\t' # Invalid option
git grep -E '\t' # Does the same as without the -E

And there doesn't seem to be an option for substituting in your own grep program. Is my only option to slurp the entire contents and then use GNU grep on the results?

Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

3 Answers3

47

You can work around this by typing a literal tab into your command:

# type it with ^V then tab
git grep '  '
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    :D well, yes. What's the Bash meaning of ctrl-v? – three Mar 22 '12 at 19:49
  • 4
    @three: That's not clear? Like I said, if you type ^V then tab, you get a literal tab. It interprets the next keystroke verbatim (literally), rather than giving it special interpretation. – Cascabel Mar 22 '12 at 20:55
40

I cannot find a regexp notation for a tab that will work, but it will happily search for a literal tab if you can pass one in. In bash, that would be

git grep $'\t'
Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
  • 2
    Such syntax also worked for grepping carriage returns: `git grep $'\r'`. – Andrew Grimm Nov 11 '11 at 04:01
  • If you want need to do an even more specific search, such as using a regex to match the beginning of a line, then you can also do git grep '^'$'\t\t' – Geuis Apr 15 '16 at 19:02
1

On Windows, the following works in PowerShell:

git grep "`t"
bannus
  • 405
  • 4
  • 6