Skip to content

Instantly share code, notes, and snippets.

@offby1
Forked from zkat/gist:1240784
Created September 25, 2011 16:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save offby1/1240799 to your computer and use it in GitHub Desktop.
Save offby1/1240799 to your computer and use it in GitHub Desktop.
git-grep in emacs
;; There's something similar (but fancier) in vc-git.el: vc-git-grep
;; -I means don't search through binary files
;; --no-color, oddly enough, is required to allow emacs to colorize the output
(defcustom git-grep-switches "--extended-regexp -I -n --ignore-case --no-color"
"Switches to pass to `git grep'."
:type 'string)
(defcustom git-grep-default-work-tree (expand-file-name "~/work/adtrack")
"Top of your favorite git working tree. \\[git-grep] will search from here if it cannot figure out where else to look."
:type 'directory
)
(when (require 'vc-git nil t)
;; Uncomment this to try out the built-in-to-Emacs function.
;;(defalias 'git-grep 'vc-git-grep)
(defun git-grep (command-args)
(interactive
(let ((root (vc-git-root default-directory)))
(when (not root)
(setq root git-grep-default-work-tree)
(message "git-grep: %s doesn't look like a git working tree; searching from %s instead" default-directory root))
(list (read-shell-command "Run git-grep (like this): "
(format (concat
"cd %s && "
"git grep %s -e %s")
root
git-grep-switches
(let ((thing (and
; don't snarf stuff from the
; buffer if we're not looking
; at a file. Perhaps we
; should also check to see if
; the file is part of a git
; repo.
buffer-file-name
(thing-at-point 'symbol))))
(or (and thing (progn
(set-text-properties 0 (length thing) nil thing)
(shell-quote-argument (regexp-quote thing))))
"")))
'git-grep-history))))
(let ((grep-use-null-device nil))
(grep command-args))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment