Emacs: keep region selected after operation

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.


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.

Tags:

Emacs