Emacs: how to re-mark a previously marked region
If the region just got unmarked (which Emacs calls "deactivated"), C-xC-x will remark (a.k.a. "re-activate") it.
The script snippets below will bind key-chords F6
, C-F6
, C-S-F6
to manager markers (vs mark). Markers move and shrink and grow, as you change text around and between them. You can use whichever keys you prefer. Put the code into your ~/.emacs
config file.
- First establish the bounds of the intitial region (mark -- point).
- Then press
F6
to set markers to the bounds of that region. - Do whatever you need to do...
- Re-establish the bounds of the modified region by pressing
C-F6
.Repeat *"Do.." as need be - When you have finished use
C-S-F6
to clear the region markers.
(global-set-key (kbd "<f6>") 'set-markers-for-region)
(defun set-markers-for-region ()
(interactive)
(make-local-variable 'm1)
(make-local-variable 'm2)
(setq m1 (copy-marker (mark)))
(setq m2 (copy-marker (point))))
(global-set-key (kbd "<C-f6>") 'set-region-from-markers)
(defun set-region-from-markers ()
(interactive)
(set-mark m1)
(goto-char m2))
(global-set-key (kbd "<C-S-f6>") 'unset-region-markers)
(defun unset-region-markers ()
(interactive)
(set-marker m1 nil)
(set-marker m2 nil))
Unfortunately, there is no region history feature in Emacs. There is the function pop-mark
, which restores the mark to a previous location taken from the variable mark-ring
, but it does not set point and thus is useless for restoring old regions. If you are adept at Emacs-Lisp, you could advise function set-mark
with code that maintains a region ring (similar to the variable mark-ring
). Then you could implement a pop-region
function that behaves similarly to pop-mark
.