1

The delete-region command in Emacs doesn't behave as expected. It sends the region to the kill-ring, even though it shouldn't.

While deleting one character at a time with delete-backward-char works as expected, i.e. it doesn't send the character to the kill-ring. delete-region sends the highlighted text to the kill-ring. Note that delete-forward-char also works fine. I have identified the problematic behavior by directly calling the command, using M-x delete-region.

I simply wish to delete a region without sending the content of that region to the kill-ring. This follows a litany of queries of a similar flavor. I have already consulted several posts on this topic, including 136581, 637351, 17914475, and 1257365, but none of the proposed solutions work because they tend to solve the problem by referring to using the delete-region command. I have also checked the relevant entry in the emacswiki, but this is not addressing the issue, either.

Here is the description of the command within Emacs:

delete-region is an interactive built-in function in ‘C source code’.

(delete-region START END)

Delete the text between START and END. If called interactively, delete the region between point and mark. This command deletes buffer text without modifying the kill ring.

My experience is that this description is incorrect. I'm using Emacs 25.2.2 on Ubuntu 18.04.

Any help would be very much appreciated.

Drew
  • 29,895
  • 7
  • 74
  • 104
Nicomachus
  • 39
  • 4
  • Is the variable `select-enable-primary` set to `t` by any chance? That's the only thing I can think of that would explain this behaviour. It means that whenever you select a region, it gets added to the kill ring, regardless of whether you delete it afterwards. – legoscia Nov 30 '18 at 09:31
  • 3
    Can you replicate the problem starting from `emacs -Q` ? – phils Nov 30 '18 at 09:49
  • What @phils said. `delete-region` does not copy text to the `kill-ring`. Something in your setup is causing it to do that - probably something in your init file, but possibly something in your `site-lisp.el` file, or even possibly something in your Emacs build (unlikely, as it would have been reported). Start by `emacs -Q` to question your init file. If that's not the problem, check `site-lisp.el`. If that's not the problem then use `M-x report-emacs-bug` to report the problem. – Drew Nov 30 '18 at 16:07
  • Thanks a lot. Both @phils and @Drew are entirely right. The problem doesn't arise when I use `emacs -Q`. – Nicomachus Nov 30 '18 at 16:30
  • Regarding @legoscia's suggestion, however, I couldn't identify any mention of `select-enable-primary` in my .emacs file. I could post it, if you want, but it is pretty long, as I have accumulated lots of commands over the years. – Nicomachus Nov 30 '18 at 16:31
  • You could use https://github.com/Malabarba/elisp-bug-hunter to bisect your configuration file and find the bit that causes the problem. – legoscia Nov 30 '18 at 16:58
  • Bisect your init file to find the culprit. You can use what @legoscia suggested or just use `comment-region` to comment out 1/2, then 3/4, 7/8, 15/16,... to narrow down you init file to the problematic part. It's a binary search, so it's quick. – Drew Nov 30 '18 at 17:19
  • I have found the problem. Thank you very much for your help. In fact, this was pretty straightforward. It was due to a recent change I had implemented. I was trying to get the highlighting function of the mouse *outside* of emacs to save the highlighted text into the clipboard, so that I could yank it, once inside emacs. Hence, I had selected the following options: `(setq x-select-enable-primary t)` and `(setq x-select-enable-clipboard nil)` which did what I wanted, but it also messed up the general yank function. Thanks a lot. It's been very instructive. – Nicomachus Dec 02 '18 at 09:29
  • Please consider deleting your question, or editing it and providing an answer, if you feel the Q&A could help others. Thx. – Drew Dec 24 '18 at 18:06

1 Answers1

1

In fact, it was pretty straightforward. I just needed to retain the default values for the following two commands.

x-select-enable-primary

x-select-enable-clipboard

In an effort to obtain a better behavior of the yank command outside of Emacs, I ended up compromising the behavior of the yank command inside Emacs. I would advise newbies not to mess up with these particular commands, and to keep the default values.

Nicomachus
  • 39
  • 4
  • It's why we should always test things with "emacs -q" before saying some emacs stuff is misbehaving ;) – learner Oct 20 '22 at 11:45