In Emacs 25 how can I save the text selection and keep the selection highlighting when using Transient Mark mode?
If I use kill-ring-save
, the highlighting is switched off and I have to switch it on with exchange-point-and-mark
.
In Emacs 25 how can I save the text selection and keep the selection highlighting when using Transient Mark mode?
If I use kill-ring-save
, the highlighting is switched off and I have to switch it on with exchange-point-and-mark
.
The function kill-ring-save-keep-selection
defined in the following emacs-lisp snippet works like kill-ring-save
but keeps the selection. I've bound it to M-W in contrast to M-w for kill-ring-save
.
(byte-compile
`(defun kill-ring-save-keep-selection (&rest args)
"Just like `kill-ring-save' with arguments ARGS but keep selection."
,(interactive-form 'kill-ring-save)
(let (deactivate-mark)
(apply 'kill-ring-save args))))
(global-set-key (kbd "M-W") #'kill-ring-save-keep-selection)
The following is based on Tobias's answer but use Advising instead:
(define-advice kill-ring-save (:around (old-fun &rest args) highlight)
"Save the text selection and keep the selection highlight."
(let (deactivate-mark)
(apply old-fun args)))
If you need to undo it, use
(advice-remove 'kill-ring-save 'kill-ring-save@highlight)