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.

halloleo
  • 1,215
  • 9
  • 23
  • 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](https://www.gnu.org/software/emacs/manual/html_node/emacs/Secondary-Selection.html). See [Secondary Selection](https://www.emacswiki.org/emacs/SecondarySelection) on Emacs Wiki. – Drew Jan 09 '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 09: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 Answers2

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)
Tobias
  • 32,569
  • 1
  • 34
  • 75
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)
xuchunyang
  • 14,302
  • 1
  • 18
  • 39