2

This question already has an answer here:

(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?

marked as duplicate by Drew, Community Dec 21 '17 at 21:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3
(defun my-delete-backward-word ()
  (interactive "*")
  (push-mark)
  (backward-word)
  (delete-region (point) (mark)))
  • Wow! Thank you! I've been suffering with this for a few years now, but no more. – Alex L Dec 21 '17 at 20:09
  • I started using windows 10 and emacs 26.1 and it looks like it stopped working. – Alex L Dec 12 '18 at 18:58
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.

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