There is a vc-git-grep mode in Emacs already (once you load the appropriate git.el etc.). However, I found it obnoxious for a number of reasons, such as being interactive and no control of the parameters to give git (e.g. to turn off the global Pager option). So, I hacked up a trivial little Lisp command:
(defun git-grep (search) "git-grep the entire current repo" (interactive (list (completing-read "Search for: " nil nil nil (current-word)))) (grep-find (concat "git --no-pager grep -P -n " search " `git rev-parse --show-toplevel`")))
Two comments on this code:
- It is still interactive, and will default the search to whatever is under the cursor currently. You can edit that in the mini-buffer, of course. In addition, it turns off the global Pager.
- More important (to me at least) is that it searches the entire git repository, and not just whatever the current directory is. That is the git rev-parse piece of the code.
With this defined, it's easy to create a global key, e.g.
(define-key esc-map "[" 'git-grep)