Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial equivalent of git grep

Tags:

grep

mercurial

Is there an equivalent to git grep in Mercurial? That is, search for text only in the working copy, in files tracked by Mercurial. (hg grep searches the repository history.)

like image 406
Matt R Avatar asked Dec 12 '14 15:12

Matt R


3 Answers

hg files "set:grep(regex_goes_here) and not binary()"

See filesets documentation for more information. Briefly, this prints all tracked files which match the given regex and are not binary files (do not contain NUL bytes).

like image 199
Kevin Avatar answered Nov 05 '22 05:11

Kevin


Add this alias to ~/.hgrc:

[alias]
grepcwd = ! $HG grep -r 'reverse(::.)' "$1" .

Then use hg grepcwd foobar the same way you would use git grep foobar.

Original source: http://www.emilsit.net/blog/archives/git-is-more-usable-than-mercurial/

like image 1
nocnokneo Avatar answered Nov 05 '22 04:11

nocnokneo


An upgrade to Kevins answer: Sadly hg files "set:grep(regex_goes_here) and not binary()" does only return the file but does not allow to peak into the found line like git grep does.

So I created an alias. Put the following lines into your ~/.bashrc:

alias hggrep='f(){
  FILES_HG=$(hg files "set:grep(\"$1\") and not binary()")
  for file_grep in $FILES_HG; do
    grep --color -Hn "$1" "$file_grep"
  done
  unset -f f
}
f'

Use like this: hggrep foobar

like image 1
Jakobimatrix Avatar answered Nov 05 '22 04:11

Jakobimatrix