14

Emacs always copies killed/deleted content to the clipboard. I often need to copy and paste content into Emacs but when I delete existing content from Emacs before pasting, the content I would like to paste is lost.

The only solution I found is to use

(setq save-interprogram-paste-before-kill t)

in order to make sure content copied outside of Emacs stays available in the kill-ring, and people with similar problems seem to be satisfied with this solution. What bothers me about it is that I have to type C-y followed by one or more repetitions of M-y to get to the content I want to paste.

So my question is: How can I stop Emacs from copying content to the clipboard when I kill/delete it (excluding cases where I delete a region with C-w)?

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • 1
    Within the last 2 to 3 weeks there were two threads dealing with a similar issue, but there appears to be a difference between how OSX handles it versus other systems. What OS are you using, please. – lawlist Jun 13 '14 at 01:18
  • @lawlist I am using Kubuntu. – Hi-Angel Jun 13 '14 at 01:19
  • 1
    So for example, You press M-d to delete a word and this deleted word is being copied to clipboard and you want to prevent this from happening, am I right? or is that you press Backspace and the deleted text is being copied to clipboard? – Carlo Espino Jun 13 '14 at 05:25
  • @CarloRodríguez Both cases. I don't want a text to be copied to the clipboard at all, except when I am cut it with `c-w`. – Hi-Angel Jun 13 '14 at 05:40
  • 1
    I just can say you can use [TinyEat](http://www.emacswiki.org/emacs/TinyEat) witch has functionality to delete without saving among other things that you may find useful, so I have `(global-set-key "\M-d" 'tinyeat-forward-preserve)` and `(global-set-key (kbd "C-k") 'tinyeat-kill-line)` but for the case of pressing Backspace and the deleted text being copied to clipboard to me that's pretty weird because that doesn't happens with my Emacs – Carlo Espino Jun 13 '14 at 06:36
  • 1
    Just to clarify: What bothers you about the `save-interprogram-paste-before-kill` solution is that if you copy some content `X` outside of Emacs and then delete some words/lines/etc. inside of Emacs *before* pasting you have to type `C-y` *followed by one or more repetitions of* `M-y` to get to `X`. Is that correct? – itsjeyd Jun 13 '14 at 06:54
  • 1
    use a clipboard manager then it's no problem if something copied to the clipboard, because you can retrieve past clipboard items from a menu – Tom Jun 13 '14 at 08:53
  • @CarloRodríguez, does you meant that you never use such useful keys, as `BackSpace` and `c-BackSpace`? Why?It's just as `c-d` and `m-d`, but reverted, very comfortable. @itsjeyd, yes, you are correct. I almost don't see an applications to use a killed into a clipboard content, most of the time it just disturbs me, as it overwrites something important there. @Tom I am using Klipper, but in our case this doesn't differ anyhow from the using `(setq save-interprogram-paste-before-kill t)`. – Hi-Angel Jun 13 '14 at 11:55
  • @JamesKingsbery sorry that I am write it in comments, but I don't see another way. I am partially disagree with an editing you approved, because 1)write alone code line in the middle of a phrase more elegant with just emphasizing (I mean as it was in the old text), 2) The P.S. now doesn't carry an useful information at all: in the old one I wanted to know, how could a people work with this (may be I am just missing some trick or hack, that all another knows), but now it looks like I am just apologize for something. Hm, strange, seems I can't write more that one user here... – Hi-Angel Jun 13 '14 at 16:01

4 Answers4

12

First off: Emacs has its own internal "clipboard" called "kill ring" which is separate from the system clipboard.

To make sure the system clipboard always has the latest content you copied outside of Emacs, add

(setq x-select-enable-clipboard nil)

to your .emacs file. According to the Emacs manual, this will

prevent kill and yank commands from accessing the clipboard [...].

Irrespective of whether you've killed text inside of Emacs after copying content outside of it, you can then use the command x-clipboard-yank to insert the contents of the clipboard into the current buffer. If you want, you can set up a global key binding for this command via

(global-set-key (kbd "C-c y") 'x-clipboard-yank)

If necessary, replace C-c y with a key binding of your choosing.

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
  • Is there a command to modify the clipboard content despite the `(setq x-select-enable-clipboard nil)`? I mean, in order to bind to a `c-w` key, so I can cut the selected text in the clipboard. – Hi-Angel Jun 13 '14 at 16:31
  • Funny: the internal Emacs documentation doesn't know about the `x-clipboard-yank` at all, but it works anyway... – Hi-Angel Jun 13 '14 at 16:43
  • 1
    `clipboard-kill-region` (for killing) and `clipboard-kill-ring-save` (for copying) should work. And you can access the built-in documentation for `x-clipboard-yank` via `C-h f x-clipboard-yank RET`. – itsjeyd Jun 13 '14 at 16:44
  • 1
    Excelent! So, to sum up: I end up with the following configs: `(setq x-select-enable-clipboard nil) (global-set-key (kbd "C-y") 'x-clipboard-yank) (global-set-key (kbd "C-w") 'clipboard-kill-region) (global-set-key (kbd "M-w") 'clipboard-kill-ring-save)` – Hi-Angel Jun 13 '14 at 16:51
  • I'm back after a while to add that now I also added a keybind `(global-set-key (kbd "s-y") 'yank)`. It is yanks from the kill-ring. I found that the kill-ring sometimes is useful, it is certainly deserves a *separate* keybinds. – Hi-Angel Jul 16 '14 at 12:05
  • 1
    x- prefixed variables are now obsolete. Use select-enable-clipboard and so on instead. – elig May 06 '21 at 20:01
4

These two settings prevent X clipboard contamination. All kill rings stay intact inside Emacs.

  (setq x-select-enable-clipboard nil)
  (setq x-select-enable-primary nil)
mike3996
  • 17,047
  • 9
  • 64
  • 80
  • I just tried after this configuration was set to delete the text with *m-d*. Alas, the text was copied in the clipboard anyway. – Hi-Angel Jun 13 '14 at 05:38
  • @YagamyLight How did you "set this configuration"? I tested the code provided in this answer on a KDE system and `(setq x-select-enable-clipboard nil)` successfully prevents deleted content from getting stored in the clipboard. – itsjeyd Jun 13 '14 at 14:41
  • @itsjeyd hmm... Strange, but this doesn't work for me at all. I tried to copy the content with m-w, and after this both the m-d and m-backspace combinations overwrite the copied text. Here's my .emacs config: http://pastebin.com/GCRxngrG – Hi-Angel Jun 13 '14 at 15:13
  • @itsjeyd I just found a funny thing. This does not really overwrite the clipboard right now, if I try to paste the text from there in any other application, the text remains old! The problem remains only in the Emacs: the `c-y` key paste only the last text that I deleted, not the one from the clipboard... – Hi-Angel Jun 13 '14 at 15:23
  • @YagamyLight Emacs has its own internal "clipboard" called ["kill ring"](https://www.gnu.org/software/emacs/manual/html_node/emacs/Kill-Ring.html) which is separate from the system clipboard. That's why you get the text you last deleted when you type `C-y`. – itsjeyd Jun 13 '14 at 15:56
  • @itsjeyd well, then if additionally to this answer, someone also write how to force the `c-y` and `c-w` to work with the system clipboard, the question will be solved. Funny, the smthng tag doesn't work in comment... – Hi-Angel Jun 13 '14 at 16:06
1

Use delete-region. Commonly commands having delete in their names don't store the stuff in kill-ring.

Andreas Röhler
  • 4,804
  • 14
  • 18
  • 1
    But then I am lose various good keybindings for deletion. As I just looked at the documentation, here is few commands, that have a "delete" in those names. – Hi-Angel Jun 13 '14 at 05:36
1

To solve the specific problem of needing to delete something before pasting in the replacement text, just use delete-selection-mode. This makes it so that the region is deleted when you paste.

Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
  • Yeah, it might work, ⁺¹. Not a scalable solution though, because e.g. in `evil-mode` I use kill-ring very extensively, however moments when I really need the killed content in system clipboard are whole order of magnitude rarer. And I also use `klipper` *(a clipboard manager)*, and prefer the clipboard history to have rather something useful. – Hi-Angel Jun 22 '17 at 04:52