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.)
Mercurial equivalent of git grep
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).
Does this require a min version of Mercurial, or a specific extension? I couldn't get
hg files
to work on v2.6.3 (unknown command 'files'
) –
Photography @Matt: shrug it's documented to work, so I'd assume it's in the latest stable version... –
Doretha
@Matt: I've determined that you can use
hg locate
in much the same way under older versions of hg. –
Doretha Mercurial 3.2 is required. The WhatsNew page says: "files: add new command unifying locate and manifest functionality". –
Unbend
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/
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
© 2022 - 2024 — McMap. All rights reserved.
grep -r
is the closest I know (or similar forack
etc.). – Farisgit grep
convenient). – Photographyack
avoids the.hg
files but yeah. You could get the list of tracked files fromhg
and pass those togrep
explicitly (assuming you don't have too many). – Faris