7

I want to overwrite text by yank as following. Is there any way to do this?

kill-ring:

text-i-want-to-paste

Before:

abcdefghijklmnopqrstuvwxyz
^
corsor

After:

text-i-want-to-pasteuvwxyz
Drew
  • 29,895
  • 7
  • 74
  • 104
Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43

3 Answers3

8

Turn on delete-selection-mode. Then select the text to replace. Then hit C-y. With delete-selection-mode enabled, you just type to replace selected text, as is usual outside Emacs. And C-y also replaces it.

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

You can also use defadvice. Then this will only work when overwrite-mode is on:

(defadvice yank (before yank-if-overwrite)
  (if (bound-and-true-p overwrite-mode)
      (delete-char (length (current-kill 0))))
  )
(ad-activate 'yank)
rerx
  • 1,133
  • 8
  • 19
nopede11
  • 297
  • 3
  • 7
5

Here:

(defun crazy-yank ()
  (interactive)
  (delete-char (length (current-kill 0)))
  (yank))

(global-set-key (kbd "C-M-y") 'crazy-yank)
abo-abo
  • 20,038
  • 3
  • 50
  • 71