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.
save-interprogram-paste-before-kill
?kill
means "delete plus put into the kill ring" wheredelete
means "just delete without affecting the kill ring", so you can call your functionbackward-delete-word
.save-interprogram-paste-before-kill
ist
, original value wasnil
. I set it with(setq save-interprogram-paste-before-kill t)
before binding the key to the function.save-interprogram-paste-before-kill
set to nil. Or better, could you try withemacs -Q
and only your function definition?