Skip to content

Instantly share code, notes, and snippets.

@zkat
Last active September 27, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zkat/1240784 to your computer and use it in GitHub Desktop.
Save zkat/1240784 to your computer and use it in GitHub Desktop.
git-grep in emacs
(defun git-grep (regexp)
(interactive "sSearch in git repo: ")
(grep (format "GIT_PAGER='' git grep -nH --no-color -i \"%s\" -- $(git rev-parse --show-toplevel)" regexp)))
(global-set-key (kbd "C-x ?") 'git-grep)
@offby1
Copy link

offby1 commented Sep 25, 2011

Oh, the irony

(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" defa
ult-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