Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: How to yank the last yanked text regardless of subsequent kills?

Tags:

emacs

kill

yank

I often find myself repeatedly yanking something after doing some kills and it becomes a process like:

  1. C-y
  2. C-y M-y
  3. C-y M-y M-y
  4. 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?

like image 277
Reed G. Law Avatar asked Apr 28 '11 18:04

Reed G. Law


People also ask

How do you yank in Emacs?

If you want to yank the region you killed before the last one, hit Ctrl-y to yank, and then Meta-y to yank the previous kill instead of the one you just retrieved.

What is the command to cut an entire Linein Emacs?

Killing by Lines The simplest kill command is C-k . If given at the beginning of a line, it kills all the text on the line, leaving it blank. When used on a blank line, it kills the whole line including its newline. To kill an entire non-blank line, go to the beginning and type C-k twice.

How do you kill multiple lines in Emacs?

To kill an entire non-blank line, go to the beginning and type C-k twice. More generally, C-k kills from point up to the end of the line, unless it is at the end of a line. In that case it kills the newline following point, thus merging the next line into the current one.

What is kill ring?

The kill ring is a list of blocks of text that were previously killed. There is only one kill ring, shared by all buffers, so you can kill text in one buffer and yank it in another buffer. This is the usual way to move text from one buffer to another.


2 Answers

Don't use the kill ring; put the text into a register instead. C-x r s a to store the region's text into (say) register "a"; then C-x r i a to insert it elsewhere.

like image 60
Sean Avatar answered Oct 01 '22 09:10

Sean


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.

(defun jp/yank (&optional arg)   "Yank and save text to register Y"   (interactive)   (set-register ?Y (current-kill 0 t))   (yank arg))  (defun jp/yank-pop (&optional arg)   "If yank-pop fails, then insert register Y"   (interactive)   (condition-case nil       (yank-pop arg)     (error (insert (get-register ?Y)))))  (global-set-key (kbd "M-y") (quote jp/yank-pop)) (global-set-key (kbd "C-y") (quote jp/yank)) 
like image 40
Juancho Avatar answered Oct 01 '22 10:10

Juancho