1

I often do something like this:

  1. Copy some stuff
  2. Go to where I need to paste it
  3. Paste it (C-y)
  4. Kill the rest of the line (C-k)
  5. Select what I just pasted, and copy it again
  6. Repeat steps 2-5

The whole "re-select what I just pasted" thing is not sensible, I know. On the other hand, I like the Emacs pasting behavior, so I don't want to enable all of what goes on with delete selection mode. There is also a whole collection of increasingly complicated routines you can write or borrow to rethink the kill-ring, but that too seems less about idioms and more about Emacs wrenching.

What is the most efficient (by keystroke count) way to accomplish this regular task, without building a complicated setup or changing to delete-selection-mode?

Drew
  • 75,699
  • 9
  • 109
  • 225
bright-star
  • 839
  • 9
  • 17
  • Instead of re-copying, yank (paste) twice (`C-y` then `M-y`) to skip to the second-to-last item in the kill ring. – Juancho May 26 '17 at 16:30
  • I would say that https://stackoverflow.com/a/5824302/319698 and https://stackoverflow.com/a/5823525/319698 are idioms, no elisp required. – npostavs May 26 '17 at 20:53
  • @npostavs Probably, but I wanted to get a canonical question into this site specifically. – bright-star May 26 '17 at 21:08
  • You should probably clarify if we're repeating just once, or `n` times. – npostavs May 26 '17 at 22:37
  • Sounds like you might just be able to do a search and replace. – db48x May 26 '17 at 23:00
  • What @npostavs said - the first URL. – Drew May 27 '17 at 00:36
  • I'm voting to close this question as off-topic because it duplicates a question on StackOverflow. This is not SE policy, but in a case like this I think it makes sense. – Drew May 27 '17 at 00:44
  • @Drew So when does emacs.SE ever actually justify adding a question (especially canonical), considering that "every" emacs.SE question eventually boils down to an elisp programming problem? (Happy to move that discussion to meta) – bright-star May 28 '17 at 00:55
  • My own feeling is that one can post an Emacs question to emacs.SE or to StackOverflow or to superuser.SE or whatever. No problem. Choose one (I'm not in favor of *cross-posting*). People choose where to post based on different things, perhaps the most important being the expected set of readers and answerers. The question here is about posting a duplicate question, which (to me) is akin to cross-posting, even if the time and OP (poster) are different. No rule against it, but I think it would be good to discourage it. The question will likely remain open here. Just expressing one opinion. – Drew May 28 '17 at 01:38

2 Answers2

2

[Posting this again here, but really think we ought to consider a policy of being able to close a question that duplicates one on StackOverflow.]


As it says here:

  1. 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 library second-sel.el).

  2. 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 then M-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).


See also the other answers to the S.O. question - using a register, for example.

JeanPierre
  • 7,323
  • 1
  • 18
  • 37
Drew
  • 75,699
  • 9
  • 109
  • 225
1

The problem you are running into here is that C-k replaces what you had in the kill ring, requiring you to go back and copy it again. To prevent this, you just need a way to delete the text you are replacing without saving it in the kill ring.

This can be done by selecting the text to delete, then pushing Backspace. This deletes the text without overwriting what you already had in the kill ring.

Jeff Wilhite
  • 111
  • 3