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?

2
  • 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
    Commented Aug 26, 2021 at 1: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 for more info.
    – Zargles
    Commented Dec 23, 2022 at 11:07

2 Answers 2

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))))
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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