I often find myself repeatedly yanking something after doing some kills and it becomes a process like:
- C-y
- C-y M-y
- C-y M-y M-y
- C-y M-y M-y M-y
Each time I kill some text it pushes the first kill back in the kill ring so that I need to cycle through all the kills to return to text I want to yank. What I want to do is repeatedly yank the same text while killing text in-between yanks. Is this possible?
I am trying to hack along the line of using a minor mode. Let's call this
delete-mode
. Once you get into delete mode, kill commands (kill-line
,kill-paragraph
,kill-word
, ...) will change their behavior so that thekill-region
part of their commands will be replaced bydelete-region
, and new material will not be added to the kill ring. While in this mode, the kill ring will stay constant. When you switch back out of this mode, the behaviour returns to normal.The following is an incomplete code attempting to implement what I wrote above. It works correctly in switching to delete mode, but it has problem switching back (turning the minor mode off). Any help fixing this would be appreciated.
This is a strange hack, but may help.
The first time you use
M-y
you normally get an error (no previous yank). So the idea is that this first time you get the last yank instead of the last kill.For storing that last yank I use the 'Y' register in this example.
These 2 functions would wrap around yank and yank-pop. You expect bugs, I expect suggestions.
A Better Yank-Pop Implementation
This defines a better yank-pop implementation which tries to fix the increasing yank-pop problem.
It only overrides the function "current-kill". Due to the modular design of yank, yank-pop, and the kill ring variables and functions in Emacs, it turns out that overriding "current-kill" is all that is necessary to get the behavior we want.
The desired behavior is that (1) killing something still puts it at the front of the kill ring, but now (2) yanking or yank-popping something also puts it at the front of the kill ring (3) we preserve the ability of yank-pop to give the appearance of moving through the kill ring by incrementing a global variable and using this to replace the last yank-pop'ped item back where it was. This also means that (4) items which are transitionally yanked (i.e. items placed by yank or yank-pop commands, where the next command is a yank-pop) ultimately get to stay where they are in the kill ring.
the code:
If you want to repeatedly yank the same text, use the secondary selection instead of the region or killed text.
What's missing from vanilla Emacs is a key binding to yank the secondary selection. I use
C-M-y
for that (see librarysecond-sel.el
).To get direct access to any kills in the kill ring, use
M-y
with Browse Kill Ring or with Icicles. In both cases,M-y
at top level gives you access to all entries in the kill ring.And if you use library
second-sel.el
then, in addition to the kill ring, you have access to a ring of your past secondary selections.And if you use library
second-sel.el
and Icicles thenM-y
yanks an entry from the the ring you last yanked from (kill ring or secondary-selection ring).And if you use library browse-kill-ring+.el then the kill-ring browser gives you access to an alternative ring also (which, by default, is the ring of secondary selections if you use library
second-sel.el
).I use
with keybinding 'M-y'. There is also helm support.
offers more solutions.
You could use
M-x delete-region
instead to kill the text, possibly binding it to a key if you want to use it a lot.