Function: vc-git-grep
vc-git-grep is an interactive and byte-compiled function defined in
vc-git.el.gz.
Signature
(vc-git-grep REGEXP &optional FILES DIR)
Documentation
Run git grep, searching for REGEXP in FILES in directory DIR.
The search is limited to file names matching shell pattern FILES.
FILES may use abbreviations defined in grep-files-aliases, e.g.
entering ch is equivalent to *.[ch]. As whitespace triggers
completion when entering a pattern, including it requires
quoting, e.g. C-q (quoted-insert)<space>.
With C-u (universal-argument) prefix, you can edit the constructed shell command line
before it is executed.
With two C-u (universal-argument) prefixes, directly edit and run grep-command.
Collect output in a buffer. While git grep runs asynchronously, you
can use C-x ` (next-error) (next-error), or RET (compile-goto-error) in the grep output buffer,
to go to the lines where grep found matches.
This command shares argument histories with M-x rgrep (rgrep) and M-x grep (grep).
Probably introduced at or before Emacs version 27.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/vc/vc-git.el.gz
;; Derived from `lgrep'.
(defun vc-git-grep (regexp &optional files dir)
"Run git grep, searching for REGEXP in FILES in directory DIR.
The search is limited to file names matching shell pattern FILES.
FILES may use abbreviations defined in `grep-files-aliases', e.g.
entering `ch' is equivalent to `*.[ch]'. As whitespace triggers
completion when entering a pattern, including it requires
quoting, e.g. `\\[quoted-insert]<space>'.
With \\[universal-argument] prefix, you can edit the constructed shell command line
before it is executed.
With two \\[universal-argument] prefixes, directly edit and run `grep-command'.
Collect output in a buffer. While git grep runs asynchronously, you
can use \\[next-error] (`next-error'), or \\<grep-mode-map>\
\\[compile-goto-error] in the grep output buffer,
to go to the lines where grep found matches.
This command shares argument histories with \\[rgrep] and \\[grep]."
(interactive
(progn
(grep-compute-defaults)
(cond
((equal current-prefix-arg '(16))
(list (read-from-minibuffer "Run: " "git grep"
nil nil 'grep-history)))
(t (let* ((regexp (grep-read-regexp))
(files
(mapconcat #'shell-quote-argument
(split-string (grep-read-files regexp)) " "))
(dir (read-directory-name "In directory: "
nil default-directory t)))
(list regexp files dir))))))
(require 'grep)
(when (and (stringp regexp) (> (length regexp) 0))
(unless (and dir (file-accessible-directory-p dir))
(setq dir default-directory))
(let ((command regexp))
(if (null files)
(if (string= command "git grep")
(setq command nil))
(setq dir (file-name-as-directory (expand-file-name dir)))
(setq command
(grep-expand-template vc-git-grep-template
regexp files))
(when command
(if (equal current-prefix-arg '(4))
(setq command
(read-from-minibuffer "Confirm: "
command nil nil 'grep-history))
(add-to-history 'grep-history command))))
(when command
(let ((default-directory dir)
(compilation-environment (cons "PAGER=" compilation-environment)))
;; Setting process-setup-function makes exit-message-function work
;; even when async processes aren't supported.
(compilation-start command 'grep-mode))
(if (eq next-error-last-buffer (current-buffer))
(setq default-directory dir))))))