Something I do often in Emacs is to cut a bit of text, and then replace another bit with the cut text. So, say I've got the text I want to yank as the last item in my kill-ring
. I yank it into the new place, then kill the text that was already there. But now the killed text is the latest item in the kill-ring
. So next time I want to yank the first item, I have to do C-y M-y. Then the next time there are two more recent items in the kill-ring
, so I have to do C-y M-y M-y, and so on.
I'm guessing there's a better way to do this. Can someone enlighten me please?
Several alternatives:
delete-selection-mode
, and use C-d or delete to delete region without touching the kill-ring.query-replace-regexp
(C-M-%).You should use delete-region
instead of kill-region
.
delete-region
deletes the region without putting it in the kill ring. It is bind to <menu-bar> <edit> <clear>
by default.
If you only want to use default bindings without using the menu, you could use delete-rectangle
with C-x r d but it works on rectangle. It could be fine to use it on a single line like delete-region
.
One of the oldest and best kept secrets in Emacs -- dunno why: Emacs has a secondary selection.
And this is exactly what it is good for. It saves another selection of text for you to use, over and over.
Select some text, then yank the secondary in to replace it. Repeat elsewhere. Often this is more convenient, flexible, and precise than something like query-replace.
Please take a look, for your own good -- maybe it will stop being such a little-known feature... http://www.emacswiki.org/emacs/SecondarySelection
I wrote this function to pop the newest item off the kill-ring:
(defun my-kill-ring-pop ()
"Pop the last kill off the ring."
(interactive)
(when kill-ring
(setq kill-ring (cdr kill-ring)))
(when kill-ring-yank-pointer
(setq kill-ring-yank-pointer kill-ring))
(message "Last kill popped off kill-ring."))
So after I kill something I don't want to keep, I hit a key that calls this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With