index all rss twitter github linkedin email

Álvaro Ramírez

12 August 2018 M-r history search in git-commit-mode

I've grown accustomed to M-r bindings to search Emacs history. Been wanting similar functionality to search commit message history. Turns out log-edit-comment-ring has some of my local commit message history. Feeding it to completing-read gives me an easily searchable history when using a completing framework like ivy or helm:

(defun ar/git-commit-search-message-history ()
  "Search and insert commit message from history."
  (interactive)
  (insert (completing-read "History: "
                           ;; Remove unnecessary newlines from beginning and end.
                           (mapcar (lambda (text)
                                     (string-trim text))
                                   (ring-elements log-edit-comment-ring)))))

Now we bind it to M-r and we're good to go:

(bind-key "M-r" #'ar/git-commit-search-message-history git-commit-mode-map)

May also want to persist log-edit-comment-ring across Emacs sessions by adding log-edit-comment-ring to savehist variables. Also ensure savehist-mode is enabled:

(add-to-list 'savehist-additional-variables log-edit-comment-ring)
(savehist-mode +1)

m-r-commit-history.png