3

Is there a way to keep the clipboard contents when deleting entire words with M-DEL?

For instance, I'd like to delete a word (M-DEL) and then paste (C-y) what I had previously copied (with ctrl+c) from a different application. Is this possible?

Drew
  • 75,699
  • 9
  • 109
  • 225
Ricky Robinson
  • 267
  • 2
  • 10
  • In the event that the existing answers hereinbelow are insufficient to address the needs of the O.P., or a future forum reader via Google, there is a way to create a custom function that separates the Emacs clipboard from the system-wide clipboard. I personally use `C-u` with my custom function when copying or pasting to add/retrieve items to/from the system clipboard .... But, there is some defgeneric stuff that Stefan wrote a few years ago that complicates things depending upon the platform Emacs is running on, and I had to spend quite a bit of time setting up my own custom function ... – lawlist Aug 26 '21 at 01:03
  • For future readers, this can be achieved easily by unsetting the variable: `(setq select-enable-clipboard nil)`. See the emacs manual (in section Emacs (top) > Killing > Cut and Paste > Clipboard). If you want, you can instead yank to and paste from the primary selection instead (esp. Linux users) by setting the variable `(setq select-enable-primary t)`. I'm not sure if this works cross-platform though. See [this SO question](https://stackoverflow.com/questions/24196020/how-to-stop-emacs-from-contaminating-the-clipboard) for more info. – Zargles Dec 23 '22 at 11:07

2 Answers2

2

No. M-DEL is bound to the function backward-kill-word, which calls kill-region. Anything that calls kill-region puts the killed text into the kill-ring, and also into the system clipboard. This is what differentiates the “kill commands” from commands that merely delete text from the buffer.

You could call delete-region instead, or you could paste from the clipboard before killing any text.

I recommend the latter, though you may also want to know about the exchange-point-and-mark command. Yanking text will set the mark just before inserting the yanked text, so you can yank something, run exchange-point-and-mark (bound to C-x C-x by default) to go back to where you started, then call backward-kill-word. This should do what you want.

There is no backward-delete-word, but it is trivial to write it yourself if you really want it:

(defun backward-delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-word (- arg)))

(defun delete-word (arg)
  "Delete characters forward until encountering the end of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-region (point) (progn (forward-word arg) (point))))
db48x
  • 15,741
  • 1
  • 19
  • 23
2

In modern versions of Emacs, by default, delete-backward-char (DEL, i.e. BackSpace) deletes the region if it is active. So you can delete instead of killing by selecting, then pressing DEL. If you want C-d or delete (Del) to do that, bind them to delete-forward-char instead of the default delete-char.

If you enable delete-selection-mode, then most commands that insert something, including yank, delete the selection if it is active. This does what you want here, but may not be what you want in other circumstances.

Alternatively, call rotate-yank-pointer with the numeric argument 0 (i.e. M-0 M-x r o - y RET — you may need to type more characters depending on what other commands with a similar name are present in your Emacs instance). This saves the system clipboard in the kill ring. Then kill whatever you want (which clobbers the system clipboard), then access the next-to-last kill ring entry with C-2 C-y (yank with the numeric argument 2) or C-y M-y (yank then yank-pop).