Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: keep region selected after operation

Tags:

emacs

In emacs, after an operation such as comment-region, the selected region is automatically deselected.

Is there any way of disabling this behaviour?

like image 228
EoghanM Avatar asked May 02 '09 20:05

EoghanM


2 Answers

If the region is deselected, that sounds like you must be using transient-mark-mode. In transient-mark-mode, when you execute a command that operates on the region, the region is deselected (in particular, the mark remains, but it is inactive). You can reselect the region (reactivate the mark) using C-x C-x (exchange-point-and-mark).

If you want to disable the deselection entirely, you can turn off transient-mark-mode. This means that you won't get any highlighting of the current region, though if you would like to set the mark and highlight the region, you can turn on transient mark mode briefly using C-<SPC> C-<SPC> or C-u C-x C-x.

like image 152
Brian Campbell Avatar answered Oct 22 '22 04:10

Brian Campbell


Make use of the variable deactivate-mark:

(defun acg/with-mark-active (&rest args)
  "Keep mark active after command. To be used as advice AFTER any
function that sets `deactivate-mark' to t."
  (setq deactivate-mark nil))

(advice-add 'comment-region :after #'acg/with-mark-active)

In the example above, you can replace comment-region with any other function you desire.

like image 22
Arthur Colombini Gusmão Avatar answered Oct 22 '22 05:10

Arthur Colombini Gusmão