" on my Linux development server, but I just found that I canโ€™...">

'git grep' and word boundaries on Mac OS X and BSD

I regularly run git grep "\<blah\>" on my Linux development server, but I just found that I canโ€™t use \< and \> on Mac (Mac OS X 10.6.8) (can't use = it doesnโ€™t find). Is regular expression syntax different on a Mac?

I tried using git grep -E "\<blah\>" but to no avail! :-(

+4
source share
3 answers

I assume this is caused by the BSD vs Linux grep library.

See if the -w option (match pattern only on word boundary) is suitable for git grep for you:

 $ git grep -w blah 
+3
source

After dealing with this, I also found this very useful post on the BSD mailing list. So here is a (albeit rather ugly) solution:

 git grep "[[:<:]]blah[[:>:]]" 

The -w git -grep flag also works, but sometimes you want to combine only the beginning or end of a word.

Update: This has changed in OS X 10.9 "Mavericks". Now you can use \< , \> and \b . [[:<:]] and [[:>:]] no longer supported.

+3
source

You can compile git with PCRE support and use git grep -P "\bblah\b" for word boundaries.

Here is a guide to compiling git using OSX Homebrew: http://realultimateprogramming.blogspot.com/2012/01/how-to-enable-git-grep-p-on-os-x-using.html

+3
source

All Articles