3

(I feel like there should be duplicate for this, but I could not find it. Pardon me.)

Question

How can I delete word backwards without it being copied to buffer?

Explanation

I developed a habit of coping text from somewhere, coming to emacs, using M-Backspace to delete some text, and then yank (C-y). Unfortunately, it is bound to backward-kill-word, which "overwrites" whatever I just copied from another place. I am looking for delete-backward-char-like functionality, but for words (like delete-backward-word) and I want it to be bound to M-Backspace. I suspect I just don't use emacs properly or "not thinking the emacs way". Should I change my habit? If so, then how?

2 Answers2

3
(defun my-delete-backward-word ()
  (interactive "*")
  (push-mark)
  (backward-word)
  (delete-region (point) (mark)))
Andreas Röhler
  • 1,894
  • 10
  • 10
0

As an alternative to the nice solution provided by @AndreasRohler , you could use counsel-yank-pop, from the counsel package, instead of yank here. The workflow would then be:

  1. Copy text from outside emacs
  2. Move to emacs and M-backspace to delete the old text
  3. Paste with M-y, i.e., counsel-yank-pop, and choose the second option (your copied text)

This requires one additional keystroke over @AndreasRohler's solution. However, it preserves the behaviour of M-backspace, and doesn't require a new keybinding for my-delete-backward-word, so that might be a worthwhile tradeoff, depending on how frequently you want to use backward-kill-word vs my-delete-backward-word.

Tyler
  • 21,719
  • 1
  • 52
  • 92