Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When using Emacs, I commonly need to perform multiple operations on the same region (often quite large but not a well-defined object such as "entire section", "entire paragraph"). Currently, I have to manually mark the region, execute the first operation, then go back and re-mark the region, and execute the second operation, and repeat... That's very cumbersome. I wonder if there is any shortcut to re-mark a previously marked region? Or some way to keep a region marked until I tell Emacs to unmark.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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))
share|improve this answer
    
Excellent! This works. I have tried several times and it always worked as expected. Thanks. –  Truong Jul 31 '12 at 1:23
add comment

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.

share|improve this answer
    
That's unfortunate. Thanks for letting me know though. I'm not an Elisp expert, so I guess I will have to continue my laborious method. –  Truong Jul 30 '12 at 14:38
add comment

Depending on your repeated operations, you might be able to record a macro using either the "pop-global-mark" command to return to the last mark in the global mark ring or you can explicitly save your place with the emacs register comand, and then after the 2nd operation, return with the register command. The register command is bit more grunty and a few more key strokes, but I find it reliable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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