0

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 way you phrase your question is ambiguous. If you just want to keep the text highlighted and be able to yank it over and over, regardless of where point is, use the secondary selection. See Secondary Selection on Emacs Wiki. – Drew Jan 9 '18 at 15:50
  • I agree, this can be read in different ways. I meant that I just want to do a Copy-to-Clipboard, so that I can use the copied text in another application, without loosing the highlighting in Emacs. – halloleo Jan 10 '18 at 9:35
  • Consider putting that info in the question itself, for clarity. Comments can be deleted at any time. Thx. – Drew Jan 10 '18 at 14:32
2

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)
  • Thanks for this. This is exactly what I was looking for. – halloleo Jan 10 '18 at 9:31
2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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