Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recall the last region selected in emacs?

Tags:

emacs

When I hit "undo" in emacs it undoes the my edit, but I lose the selected region, and I have to go back and reselect it. Is there a way to bind undo so it'll select the last selected region

The best I can come up with was:

(global-set-key (kbd "\C-o")
                (lambda()
                  (interactive)
                  (progn (undo)
                          (exchange-point-and-mark)
                          )))
like image 797
benhsu Avatar asked Jul 13 '12 19:07

benhsu


2 Answers

You do NOT lose the region. The region remains in the yank-ring.

If you want to re-select it, you can simply call exchange-point-and-mark, which is bound by default to C-xC-x.

like image 163
alinsoar Avatar answered Sep 18 '22 14:09

alinsoar


You can always use advice to take what you have and wrap it around undo:

(defadvice undo (around reactivate-mark (&optional arg) activate)
  (let ((ma mark-active))
    ad-do-it
    ;; Reactiveate mark if it was active
    (when ma
      (exchange-point-and-mark))))
like image 38
Ivan Andrus Avatar answered Sep 16 '22 14:09

Ivan Andrus