In emacs, after an operation such as comment-region
, the selected region is automatically deselected.
Is there any way of disabling this behaviour?
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
.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With