I've been playing with vc-git-grep as pointed to by this answer How do you grep from within emacs ( git grep 'foo' | grep baa )

I have a special requirement like this:

$ git grep -e 'include' --and -e Image

components/core/app/models/contact.rb:  include HasImage
components/core/app/models/opportunity.rb:  include HasBannerImage
components/core/app/models/organization.rb:  include HasImage
components/core/app/models/organization.rb:  include HasBannerImage

Is it possible to do that with emacs vc-git-grep ?

I tried

-*- mode: grep; default-directory: "~/" -*-
Grep started at Wed Sep  6 06:59:56

git --no-pager grep -n -e Image -- *
Gemfile.lock:70:      rails-assets-Imagesloaded (= 4.1.1)
Gemfile.lock:362:    rails-assets-Imagesloaded (4.1.1)

To summarize, my question is, is it possible to manipulate the parameters passed to the git grep command?

  • You should look at the counsel package. The commands counsel-rg and counsel-git-grep are great. counsel-projectile-rg is another really good one. – eflanigan00 Sep 6 '17 at 17:25

I found the answer to my own question. It seems M-x grep is pretty powerful, in that it can handle any "grep like" shell command's output.

So you can do

M-x grep
# it will ask you for the command, paste this
git --no-pager grep -n -e 'include' --and -e Image

and voila!

  • 4
    You might consider customizing grep-command if this is the standard way you want to use M-x grep – phils Sep 6 '17 at 14:33

This feature has been recently added to Emacs (see this commit), and will probably be out on version 27.

The NEWS entry is:

*** New customizable variable 'vc-git-grep-template'. This new variable allows customizing the default arguments passed to git-grep when 'vc-git-grep' is used.

Refer to C-hv vc-git-grep-template for usage.

  • 1
    Right now this is a link-only answer, and risks being deleted. Could you explain here how to use it? It's ok if the functionality isn't released yet. – zck Oct 8 '18 at 15:59
  • 1
    @zck, Have fixed that. – phils Oct 8 '18 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.