13

Using Crtl+Backspace or Ctrl+Del adds the deleted text to the kill ring.
How can I prevent this from happening? I do not need to have every single piece of text sent to the kill ring when it gets deleted.

Side question: What's the purpose of this default setting anyway? :)

mwal
  • 111
  • 5
Daniel Hitzel
  • 507
  • 4
  • 13
  • 1
    About the side question: probably to have a backup copy of what you delete without going through `undo`. Wild guess, though. – Manuel Uberti Apr 03 '19 at 16:41

4 Answers4

10

According to the documentation:

<C-delete> runs the command kill-word (found in global-map), which is an interactive compiled Lisp function in ‘simple.el’.

It is bound to <C-delete>, M-d.

(kill-word ARG)

Kill characters forward until encountering the end of a word. With argument ARG, do this that many times.

Now, let's browse the source code:

(defun kill-word (arg)                                                                                                     
  "Kill characters forward until encountering the end of a word.                                                           
With argument ARG, do this that many times."
  (interactive "p")
  (kill-region (point) (progn (forward-word arg) (point))))

Then, inside the documentation for the kill-region function we find:

Kill ("cut") text between point and mark.
This deletes the text from the buffer and saves it in the kill ring. The command [yank] can retrieve it from there. (If you want to save the region without killing it, use [kill-ring-save].)

[...]

Lisp programs should use this function for killing text. (To delete text, use delete-region.)

Now, if you want to go further, this is some function you can use, for deleting without copying to kill-ring:

(defun my-delete-word (arg)
  "Delete characters forward until encountering the end of a word.
With argument, do this that many times.
This command does not push text to `kill-ring'."
  (interactive "p")
  (delete-region
   (point)
   (progn
     (forward-word arg)
     (point))))

(defun my-backward-delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument, do this that many times.
This command does not push text to `kill-ring'."
  (interactive "p")
  (my-delete-word (- arg)))

(defun my-delete-line ()
  "Delete text from current position to end of line char.
This command does not push text to `kill-ring'."
  (interactive)
  (delete-region
   (point)
   (progn (end-of-line 1) (point)))
  (delete-char 1))

(defun my-delete-line-backward ()
  "Delete text between the beginning of the line to the cursor position.
This command does not push text to `kill-ring'."
  (interactive)
  (let (p1 p2)
    (setq p1 (point))
    (beginning-of-line 1)
    (setq p2 (point))
    (delete-region p1 p2)))

; bind them to emacs's default shortcut keys:
(global-set-key (kbd "C-S-k") 'my-delete-line-backward) ; Ctrl+Shift+k
(global-set-key (kbd "C-k") 'my-delete-line)
(global-set-key (kbd "M-d") 'my-delete-word)
(global-set-key (kbd "<M-backspace>") 'my-backward-delete-word)

Courtesy of ErgoEmacs

Nsukami _
  • 6,341
  • 2
  • 22
  • 35
5

Since Emacs 24, without any configuration, you can delete any text without adding it to the kill ring by selecting it and then pressing Backspace or Delete. When the mark is active, these keys delete the region instead of just deleting the character before/after the cursor.

That is, move to one end of the text you want to delete, press Ctrl+Space, move to the other end, and press Backspace (or Delete) do delete the highlighted region. Or move to one end, hold Shift while you use movement keys to go to the other end, and press Backspace.

0

An alternative solution is to advice the functions you don't want affecting the kill-ring, in such a way that you'd save and restore the kill-ring when called interactively:

(defun my/call-interactively-inhibit-kill-ring (fun &rest args)
  "Call FUN interactively saving the kill-ring when called interactively.
Otherwise call FUN with ARGS."
  (if (interactive-p)
      (let ((kill-ring '("dummy"))  ; Dummy value in case FUN tries to append.
            (kill-ring-yank-pointer nil))
        (call-interactively fun))
    (apply fun args)))

(defun my/inhibit-kill-ring-in (cmd)
  (advice-add cmd :around #'my/call-interactively-inhibit-kill-ring))

(my/inhibit-kill-ring-in 'backward-kill-word)
(my/inhibit-kill-ring-in 'kill-word)
Michaël
  • 314
  • 1
  • 10
-1

I had the same problem and solved it more easily this way:

(global-set-key (kbd "<backspace>")
          '(lambda () (interactive) (backward-delete-char-untabify 1 nil)))

Calling (backward-delete-char-untabify 1 nil) tells Emacs to backward-delete one char (that's the 1 parameter) and that it refrains from copying to the ring (that's the nil parameter). You need a lambda (i.e. anonymous) function because you're binding a function with parameters to a key.

NVaughan
  • 1,481
  • 12
  • 27
  • Backspace does not send things to the kill ring by default, I'm not sure how this answers the question. – Michaël Dec 18 '20 at 18:39