Emacs: Delete Word Without Copying to Clipboard/kill-ring
Here's commands that delete text without copying it to clipboard/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)
Which is More Efficient? Copy to Clipboard or Not?
In emacs, deleting a word 【Alt+x d】 or line 【Ctrl+k】 will also put the deleted text into emacs's clipboard called the kill-ring.
I thought about the issue. That is, is it more efficient editing to automatically putting text to kill-ring? Does it save keystrokes?
For this purpose, i wrote the equivalent non-kill versions and gonna use it myself to experiment.
Some random comments and thoughts follows.
Initial first day experience of using the above is that its annoying, of course due to habit of 10 years with emacs ways.
Note that emacs text-erasing commands that contains the word “kill” is meant to push text into the kill-ring. So, instead of naming “my-kill-line”, more proper naming in the context of emacs is “delete-line” or “my-delete-line”, thus i've named them above.
Also included above is the command my-delete-line-backward, which is like kill-line but it deletes text from cursor point to beginning of line, as opposed to end of line. I've been using this command together with my ergonomic keyboard shortcut set for over a year now and find it convenient.
It seems logical that emacs does not provide a option for the kill commands to not push to the kill-ring. To provide that option would be breaking the original design's consistency, because text-erasing commands with “kill” in their names are supposed to work with the “kill-ring”.
But, suppose non-kill is something we absolutely need in emacs, then it can be still done without breaking design, by providing the set of new functions without the word “kill” in name. When a user sets her preference to not push erased text onto the “clipboard”, then the keybinding for these kill commands will be bound to the non-kill set of commands that has “delete” in their names.
Since about 2005 i thought about many emacs issues of its non-standard or non-conventional user interface. [see Modernization of Emacs] That is why the “kill” issue is interesting to me. For vast majority of professional programers, perhaps 99.99%, the emacs ways of pushing deleted text into the kill-ring (modern concept of “clipboard”) is unfamiliar, thus a setback.
I started to use emacs in 1998 and by 1999 i live in emacs daily. I don't remember now, but undoubtedly i was also surprised by emacs's ways of putting deleted text onto the “clipboard” in the beginning, however, i quickly adopted it and don't remember ever thought about it.
In the past few years, sometimes i also run into the problem where i don't want killed text to offset whatever i have copied to the “clipboard”. In such a occasion, of course, you select the text then hit the delete key, so that the content of last entry of the “clipboard” is still intact. Alternatively, i learned to use the emacs feature of “register” (which is another form of “clipboard”).
[see Emacs: Copy to Register]
In the end, the interesting question is whether emacs way of pushing into kill-ring on deleting word/line/sentence is more operatively efficient. By “operatively efficient”, i meant of less keystrokes or more intuitive, for general editing tasks. This can be tested at least theoretically, by imagining 2 groups of emacs users of equal experience, one group having used a emacs version such that kill-line, kill-word, backward-kill-word don't push to the kill-ring. While the other group uses the standard emacs. Then, suppose we record their keystrokes and command calls for a few months, then we can analyze the keystroke log and see whether one way is better. This may sound too complicated to carry out… but actually i think if any long time emacs user actually tried the above for at least 2 months, he can get a sense of which one is more operatively efficient. (this would be like adopting Dvorak keyboard, the first week will be extreme pain in the ass. But only after full adoption, one can truly judge)
It's my guess that the operative efficiency of the two methods actually doesn't differ that much. That is, one method might be more convenient or save keystrokes sometimes, but not always. If this is so, then emacs would be much better off, if it adopts the more widely familiar interface by not having the delete word/line/sentence shortcuts push into the kill-ring/clipboard.
Just a brief follow up on this thread in case others read this thread in the future.
After 2 days, i've ended my experiment on using deleting word/line shortcuts that doesn't push to the kill ring. My preliminary conclusion is that it's not as convenient as emacs default way, of pushing any deleted word/line into the kill-ring/clipboard.
My brief thinking is this … whether deleted text are pushed into the clipboard is a more useful interface, depends on this:
• Whenever a user does a sequence of deletes of a word or line, is the existing content in the clipboard useful?
Most of the time, the answer is no. Further, when deleting a line, often it is useful that the deleted text be pushed into the clipboard as to be immediately pasted somewhere.
my brief experience of using the non-kill version is that, whenever i delete a line and needed to paste the deleted text somewhere else, that non-kill version forced me to increases keystroke by 1 or 2. A rough guess on the frequency of deleting a line that needs to be pasted elsewhere, is about maybe 1 out of 20 of every delete word/line operation.
Also note, that the situation needing the content of clipboard intact, does not happen very often. (from past decade of emacs using, my guess is that it happens once a week) The reason must be that clipboard are often used in such a way that after something is copied, a paste operation immediately follows, and that the clipboard content is no longer useful. So, in general, it is rare that clipboard content remains in a useful state for duration longer than few seconds.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboard for Emacs
Ask me question on patreon