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? :)

1
  • 1
    About the side question: probably to have a backup copy of what you delete without going through undo. Wild guess, though. Commented Apr 3, 2019 at 16:41

4 Answers 4

12

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

4
  • thanks, that solved it. What about the side question? :p Commented May 14, 2016 at 16:36
  • 3
    @DanielHitzel Sorry, I don't have the answer to the side question :)
    – Nsukami _
    Commented May 14, 2016 at 16:37
  • 1
    So I am not alone :D Commented May 14, 2016 at 16:38
  • Do I put these into init.el?
    – Alex
    Commented Feb 7 at 0:26
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.

2
  • 1
    Well, that's not the case for me with Emacs 25.1 in MacOS.
    – NVaughan
    Commented Oct 25, 2016 at 1:52
  • Nor Emacs 29.4 on NixOS
    – shackra
    Commented Oct 8 at 13:57
1

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

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

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.