15

When switching files using the minibuffer (C-x C-f), I often use M-Backspace to delete words in the path. Emacs automatically places what I delete into the kill ring. This can be annoying, as sometime I am moving to another file to paste something, and I end up pasting part of the file path. I know there are workarounds, and the other code is still in the kill ring, etc, but I would just like to disable this functionality.

Tyler
  • 9,872
  • 2
  • 33
  • 57
drewrobb
  • 1,574
  • 10
  • 24

3 Answers3

20

Emacs doesn't have a backward-delete-word function, but it's easy enough to define one:

(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-region (point) (progn (backward-word arg) (point))))

Then you can bind M-Backspace to backward-delete-word in minibuffer-local-map:

(define-key minibuffer-local-map [M-backspace] 'backward-delete-word)
cjm
  • 61,471
  • 9
  • 126
  • 175
  • And here I've been going through life using only backspace and C-d to edit filenames in the mini-buffer. :) – andrewdski May 26 '11 at 05:19
4

See a discussion of this topic at help-gnu-emacs@gnu.org: http://lists.gnu.org/archive/html/help-gnu-emacs/2011-10/msg00277.html

The discussion boils down to this short solution:

(add-hook 'minibuffer-setup-hook'
          (lambda ()
            (make-local-variable 'kill-ring)))
katspaugh
  • 17,449
  • 11
  • 66
  • 103
Drew
  • 29,895
  • 7
  • 74
  • 104
0

You just have to replace the function called by M-<backspace>, namely backward-kill-word, with backward-delete-word, which you can easily define using the source definition of backward-kill-word found in lisp source for emacs. You do this by substituting kill-region for delete-regionas in the following code, which also defines delete-word (delete word after the cursor). You can just paste this code in your .emacs file.

(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))))
(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)))
(global-set-key (kbd "M-<backspace>") 'backward-delete-word)
(global-set-key (kbd "M-<delete>") 'delete-word)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 30 '22 at 10:21