Emacs vc-git tweaks

I use Emacs, vc, and git all pretty heavily. I know the cool kids use magit and egg, but good old vc has stuck with me through four different “new hotness” version control systems – CVS, Perforce, Subversion, and now git – and I’m sure it will still be around for many more to come.

Still, git is a beast, even compared to other big (D)VCSes, and vc doesn’t expose many of its features. A few in particular I sorely missed were staging and unstaging individual files and smarter refreshing in vc-dir buffers. Here’s the code in my .emacs that adds those features. I overrode a few existing vc key bindings; feel free to change those, of course.

;; In vc-git and vc-dir for git buffers, make (C-x v) a run git add, u run git
;; reset, and r run git reset and checkout from head.
(defun my-vc-git-command (verb fn)
  (let* ((fileset-arg (or vc-fileset (vc-deduce-fileset nil t)))
         (backend (car fileset-arg))
         (files (nth 1 fileset-arg)))
    (if (eq backend 'Git)
        (progn (funcall fn files)
               (message (concat verb " " (number-to-string (length files))
                                " file(s).")))
      (message "Not in a vc git buffer."))))

(defun my-vc-git-add (&optional revision vc-fileset comment)
  (interactive "P")
  (my-vc-git-command "Staged" 'vc-git-register))

(defun my-vc-git-reset (&optional revision vc-fileset comment)
  (interactive "P")
  (my-vc-git-command "Unstaged"
    (lambda (files) (vc-git-command nil 0 files "reset" "-q" "--"))))

(define-key vc-prefix-map [(r)] 'vc-revert-buffer)
(define-key vc-dir-mode-map [(r)] 'vc-revert-buffer)
(define-key vc-prefix-map [(a)] 'my-vc-git-add)
(define-key vc-dir-mode-map [(a)] 'my-vc-git-add)
(define-key vc-prefix-map [(u)] 'my-vc-git-reset)
(define-key vc-dir-mode-map [(u)] 'my-vc-git-reset)

;; hide up to date files after refreshing in vc-dir
(define-key vc-dir-mode-map [(g)]
  (lambda () (interactive) (vc-dir-refresh) (vc-dir-hide-up-to-date)))

See EmacsWiki’s VersionControl and Git pages for lots more info!

6 thoughts on “Emacs vc-git tweaks

  1. Woa! Somebody still using emacs… Seems emacs was too early doing everything, now people prefer vim and that python clone. Wish emacs would get more love. Especially on android there is no emacs editor that works and actively is worked on. Several vims but I refuse…

    via plus.google.com

  2. The commands that add functionality to vc-dir-mode-map need to be put on a hook?

Leave a Reply

Your email address will not be published. Required fields are marked *