Skip to content

Instantly share code, notes, and snippets.

@zkat
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkat/4398cd6fb03fd635c8f1 to your computer and use it in GitHub Desktop.
Save zkat/4398cd6fb03fd635c8f1 to your computer and use it in GitHub Desktop.
;;;
;;; Git utils
;;;
(require 'vc-git nil t)
(defcustom git-grep-switches "-E -I -nH -i --no-color"
"Switches to pass to `git grep'."
:type 'string)
(defun git-grep (regexp)
(interactive
(list (read-from-minibuffer
"Search git repo: "
(let ((thing (and 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))))
""))
nil nil 'git-grep-history)))
(let ((grep-use-null-device nil)
(root (vc-git-root default-directory)))
(if root
(grep (format "GIT_PAGER='' git grep %s -e %S -- %s" git-grep-switches regexp root))
(message "Not in a git repo."))))
(defun remove-ignored-files (list)
(remove-if (lambda (file)
(some (lambda (suffix)
(string-suffix-p file suffix))
completion-ignored-extensions))
list))
(defun get-git-files (dir)
(remove-ignored-files
(split-string (shell-command-to-string (concat "cd " dir ";" "git ls-files"))
"\n")))
(defun ido-open-in-project ()
(interactive)
(let ((root (vc-git-root default-directory)))
(if root
(find-file (concat root
(ido-completing-read "Find in project: "
(get-git-files root)
:require-match t)))
(message "Not in a git repo."))))
(defun string-suffix-p (str1 str2)
"Check if str1 ends with str2"
(unless (< (length str1) (length str2))
(string= (subseq str1 (- 0 (length str2)))
str2)))
;; Keybinding
(global-set-key (kbd "C-x ?") 'git-grep)
(global-set-key (kbd "C-x f") 'ido-open-in-project)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment