12

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

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

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

  • thanks, that solved it. What about the side question? :p – Daniel Hitzel May 14 '16 at 16:36
  • 3
    @DanielHitzel Sorry, I don't have the answer to the side question :) – Nsukami _ May 14 '16 at 16:37
  • So I am not alone :D – Daniel Hitzel May 14 '16 at 16:38
4

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.

  • Well, that's not the case for me with Emacs 25.1 in MacOS. – NVaughan Oct 25 '16 at 1:52
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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