1

Is there a way to cut text in such a way that it is only removed from the buffer when I paste it somewhere else?

I find myself often losing the text I had cut because, after I cut, I find out I need to do something else before I can paste the text where I want it.

4
  • 1
    There's no reason not to be able to yank back even if you do something else between cut and paste. If you cut something else, use C-y to paste last cut, then immediately M-y to replace with previous cut, repeat if needed (this cycles through the kill ring).
    – JeanPierre
    Commented Dec 30, 2019 at 13:34
  • @JeanPierre: Please consider posting your comment as an answer.
    – Drew
    Commented Dec 30, 2019 at 15:24
  • What @JeanPierre said. Emacs killing and yanking is not just cutting and pasting.
    – Drew
    Commented Dec 30, 2019 at 15:24
  • @Drew I understand, but I'm trying to replicate specific behaviour that I want. That's all.
    – Ivan Perez
    Commented Dec 30, 2019 at 20:19

2 Answers 2

1

While it's certainly possible to program something to do that, there's no reason not to be able to yank back previously killed text even if you do some other edits after the kill.

Note that emacs does not define cut and paste operations, but similar killing and yanking, as the "Killing and Moving Text" section of the manual states:

In Emacs, “killing” means erasing text and copying it into the “kill ring”. “Yanking” means bringing text from the kill ring back into the buffer. (Some applications use the terms “cutting” and “pasting” for similar operations.) The kill ring is so-named because it can be visualized as a set of blocks of text arranged in a ring, which you can access in cyclic order.

Subsection "Yanking" defines:

“Yanking” means reinserting text previously killed. The usual way to move or copy text is to kill it and then yank it elsewhere.

‘C-y’ Yank the last kill into the buffer, at point (‘yank’).

‘M-y’ Replace the text just yanked with an earlier batch of killed text (‘yank-pop’).

So what you can do is simply kill your text and yank it back (C-y) when you need it. If you have performed other kills in between, you can use M-y to replace the just yanked text with the previous in the kill ring. Repeating M-y will cycle through the kill ring, letting you find the wanted block of previously killed text.

Of course the kill ring cannot grow indefinitely, but it defaults to storing a maximum of 60 kills, wich is most probably much more than you need, and can be changed:

The maximum number of entries in the kill ring is controlled by the variable ‘kill-ring-max’. The default is 60. If you make a new kill when this limit has been reached, Emacs makes room by deleting the oldest entry in the kill ring.

0

As I said in my other answer, I don't think you should have to do that. But as I also said, this can be done, so here's a way to do it. It's rather straightforward and basically works for cut/pastes inside the same buffer. I haven't thought much about it, so there could be unexpected pitfalls.

  1. Select the region you want to "cut late": set mark to one end and point to the other one and run M-x jp/cut-late-select-region,

  2. Later, actually perform the cut/paste with M-x jp/cut-late-do-cut-paste.

Of course, you can bind those two commands to whatever keys you like.

(defvar-local jp/cut-late1 nil "Cut late region beginning")
(defvar-local jp/cut-late2 nil "Cut late region end")

(defun jp/cut-late-select-region (beg end)
  "Select region to be cut/pasted by jp/cut-late-do-cut-paste."
  (interactive "r")
  (pop-mark)
   (setq jp/cut-late1 beg
         jp/cut-late2 end))

(defun jp/cut-late-do-cut-paste ()
  "Cut and paste the region previously selected by
jp/cut-late-select-region."
  (interactive)
  (if (and jp/cut-late1 jp/cut-late2)
      (progn
        (kill-region jp/cut-late1 jp/cut-late2)
        (yank)
        (setq jp/cut-late1 nil
              jp/cut-late2 nil))
    (error "No cut late region defined in this buffer!")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.