Let's say I've added some text to some line:

some text▊

Then I scroll down a bit, so that the line is out of sight:

When I press undo, I don't want Emacs to delete the line above immediatedly, but only locate it:

some text▊

And when I press undo a second time it should actually undo it:

  • 1
    There is a much better alternative. Use undo-tree. It is available from elpa. Therewith you can freely wander around in your undo-activities and don't need to apply any of them. For every undo-action you can see what it would do.
    – Tobias
    Commented Jan 25, 2016 at 13:34

1 Answer 1


As I have already mentioned in the comments I would prefer undo-tree. But, with emacs there is always a way to get what you want.

The following advice of undo does what you want in most cases. I think some rare cases cannot be handled.

(defadvice undo (around first-make-change-visible activate)
  "Before actually un-doing make the changed position visible."
  (let (goal)
    (if (listp buffer-undo-list)
    (let ((top buffer-undo-list))
      (while (null (car top))
        (setq top (cdr top)))
      (setq top (car top))
       ((and (consp top)
         (number-or-marker-p (car top))
         (number-or-marker-p (cdr top)); insertion starting at (car top) and ending at (cdr top)
         (null (pos-visible-in-window-p (car top)))
         (null (pos-visible-in-window-p (cdr top)))); not visible
        (setq goal (cdr top)))
       ((and (consp top)
         (stringp (car top))
         (numberp (cdr top)); deletion
         (null (pos-visible-in-window-p (cdr top)))); not visible
        (setq goal))
       ((and (consp top)
         (null (car top))
         (symbolp (nth 1 top))
         (number-or-marker-p (nth 3 top))
         (number-or-marker-p (nth-cdr 4 top)); property change
         (null (pos-visible-in-window-p (nth-cdr 4 top))))
        (setq goal (nth-cdr 4 top))))))
    (if goal
      (goto-char goal)

Your Answer

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

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