I'd like to discard modified hunks (and ideally, view the diff before) basing on the current buffer position.

I have diff-hl so that it's quite obvious which part of a buffer are modified. Now, how to view the diff for the current hunk and/or discard it?

One way I though this might work is some way to jump to magit's view, moving to the selected hunk - this would show the diff, and then k would discard it. I was also looking at vc-mode, but didn't find anything that would fit that use-case.

2 Answers 2

  • diff-hl: diff-hl-revert-hunk
  • git-gutter: git-gutter:revert-hunk

Something like this ?

(defun vc-diff-current-hunk ()
  "Find the hunk at point in a `vc-diff' buffer and narrow it there."
  (let ((line (line-number-at-pos))
    (goto-char 1)
    (while (and (not found)
                 "^@@ -[0-9]+,[0-9]+ \\+\\([0-9]+\\),\\([0-9]+\\) @@" nil t))
      (let ((hunk-line (string-to-number (match-string 1)))
            (hunk-count (string-to-number (match-string 2))))
        (setq found (and (>= line hunk-line)
                         (< line (+ hunk-line hunk-count))))))
    (when found
           (if (ignore-errors (diff-hunk-next) t)
      (message "Hunk found"))
    (unless found
      (error "Hunk not found"))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.