1

I want to delete regions without adding them to the kill ring following this thread:

(defun backward-kill-word-no-kill-ring ()
  "Delete the previous word and remove it from the kill ring."
  (interactive)
  (backward-kill-word 1)
  (setq kill-ring (cdr kill-ring))
  )

(global-set-key (kbd "M-DEL") 'backward-kill-word-no-kill-ring)

It fails and I don't understand why. If I type abcd, copy it with Cmd-C, and yank it with C-y, Emacs inserts abcd and the kill ring, viewed with C-h v kill-ring RET, is:

(Value:
(#("abcd" 0 4
    (fontified t))
...

If I type 123, delete it with M-DEL, which calls my function, and then yank with C-y, Emacs inserts https://www.fablabs.io/labs, which I had copied 30 minutes ago and is no longer an entry in the kill ring, and the kill ring is:

Value:
(#("123" 0 3
   (fontified t))
  #("abcd" 0 4
    (fontified t))
...

Then I cut text with C-w (kill-region), yank in another buffer, and it's the same link that comes first and my cut text that comes second (after M-y).

Where does yank grab its text if not from the kill ring?

Update

Following Tobias's comment, I had save-interprogram-paste-before-kill at t, and have the same behavior when I set it to nil and start Emacs with emacs -Q. When I follow the steps above, after M-DEL of 123, Emacs inserts the last entry from the kill ring, abcd. (When I first asked the question, I may have missed that the inserted text was the last entry of the kill ring, even after searching for the string in the kill-ring information buffer.)

Further udpate

A similar question asks how to delete text without adding to the kill ring. The solution there, as the answer by Tobias here, is more elegant than my first attempt. I edited the question to understand why Emacs is using the last entry in the kill ring instead of the top entry.

Further update (10 March)

I follow the steps above with emacs -Q, copy abcd to the clipboard and yank it, copy the function and keybinding and execute it, and after M-DEL of 123, Emacs inserts the last entry from the kill ring, abcd.

The kill ring after that insertion is:

Value:
("(defun backward-kill-word-no-kill-ring ()
  \"Delete the previous word and remove it from the kill ring.\"
  (interactive)
  (backward-kill-word 1)
  (setq kill-ring (cdr kill-ring))
  )

(global-set-key (kbd \"M-DEL\") 'backward-kill-word-no-kill-ring)"
 #("abcd" 0 4
   (fontified t)))

The Emacs version is:

GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20 Version 10.14.3 (Build 18D109)) of 2019-09-02

The values of interprogram-paste-function and interprogram-cut-function are the default ones, gui-selection-value and gui-select-text. The behavior is the same if I use M-w instead of Cmd-C to copy the first piece, abcd, to the kill ring.

  • What is the value of save-interprogram-paste-before-kill? – Tobias Mar 3 at 16:43
  • 3
    Side note: Emacs follows the namiong convention that kill means "delete plus put into the kill ring" where delete means "just delete without affecting the kill ring", so you can call your function backward-delete-word. – Stefan Mar 3 at 18:37
  • @Tobias: The value of save-interprogram-paste-before-kill is t, original value was nil. I set it with (setq save-interprogram-paste-before-kill t) before binding the key to the function. – miguelmorin Mar 5 at 9:50
  • 1
    If you are still interested in finding the cause of the strange kill-ring behavior, could you try with save-interprogram-paste-before-kill set to nil. Or better, could you try with emacs -Q and only your function definition? – Tobias Mar 5 at 9:57
  • 2
    Does this answer your question? Backspace without adding to kill ring – Basil Mar 5 at 11:36
2

I don't know why your kill-ring gets messed up. It might be that it is some inter-program action that modifies the kill-ring.

But the right way to implement deleting instead of killing is:

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

Note, that I just copied the stuff from simple.el and replaced kill with delete.

| improve this answer | |

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.