12

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?

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • Not aware of any other way to do it. You might be able to save in a register but I've not used that myself. – cristobalito Sep 24 '10 at 12:27
  • You've hit upon a key problem with the kill-ring. The most recently used item is most likely to reused. So you don't want to order the ring chronologically. Enter my LRU-yank package. https://github.com/lewang/le_emacs_LRU_yank I yank useful stuff with less keystrokes with it. – event_jr Sep 19 '11 at 18:39

5 Answers5

10

Several alternatives:

  1. Turn on delete-selection-mode, and use C-d or delete to delete region without touching the kill-ring.
  2. Use C-x r s i to save text to register i, and later, C-x r i i to insert the saved text.
  3. If the pattern of texts to be replaced can be captured in a regular expression, use query-replace-regexp (C-M-%).
huaiyuan
  • 26,129
  • 5
  • 57
  • 63
6

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.

Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40
  • Thanks. I'm looking at a way to do it without having to do `M-x kill-region` each time, and I'd rather not add a new keybinding. Is there no way to do this with standard keybindings? – Skilldrick Sep 24 '10 at 12:47
  • You could do a query-replace ( M-% ) by yanking in the minibuffer the thing you want to use to replace other things : M-% thestringtoreplace RET C-y RET ! – Jérôme Radix Sep 24 '10 at 12:53
  • @Jérôme Yes, I think you're right. The only problem with that is that the replaced string must be the same each time, whereas I'd like to be able to manually replace a load of different things with the same string. I think this is one thing that's much easier to do with standard text editors (i.e. using Ctrl-C then Ctrl-V) than with Emacs. – Skilldrick Sep 24 '10 at 12:56
  • I'll leave the question open for a day just in case, but mark this as completed if I don't get any other answers. – Skilldrick Sep 24 '10 at 12:58
  • I added a word on delete-rectangle which could be used like delete-region on a single line, and delete-rectangle has a key binding by default. – Jérôme Radix Sep 24 '10 at 13:00
  • Thanks - that's exactly what I wanted! – Skilldrick Sep 24 '10 at 13:23
  • As for a default keybinding for `delete-region`, pressing Backspace over a selected region does `delete-region` instead of `kill-region` as of Emacs 24. See `delete-active-region` – Jisang Yoo Mar 15 '13 at 12:46
4

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

Drew
  • 29,895
  • 7
  • 74
  • 104
2

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.

scottfrazer
  • 17,079
  • 4
  • 51
  • 49
0

Related to this is M-x browse-kill-ring. If you use M-x anything, you can also use M-x anything-show-kill-ring.

aartist
  • 3,145
  • 3
  • 33
  • 31