Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a region in emacs with yank buffer contents?

Tags:

emacs

When I use VIM or most modeless editors (Eclipse, NetBeans etc.) I frequently do the following. If I have similar text blocks and I need to change them all, I will change one, copy it (or use non-deleting yank), select next block I need and paste the changed version over it. If I do the same thing in emacs (select region and paste with C-y), it doesn't replace the region, it just pastes at the cursor position. What is the way to do this in emacs?

like image 536
Mad Wombat Avatar asked Apr 13 '10 05:04

Mad Wombat


2 Answers

Add this to your .emacs:

(delete-selection-mode 1) 

Anything that writes to the buffer while the region is active will overwrite it, including paste, but also simply typing something or hitting backspace

like image 196
Michael Mrozek Avatar answered Oct 09 '22 01:10

Michael Mrozek


Setting delete-selection-mode, as Michael suggested, seems the most natural way to do it.

However, that's not what I do :) Instead, I put the good stuff into a "register" -- for example, register "a" -- with C-x r x a. Then I kill the other copy, and copy the register into the same spot with C-x r g a.

This is convenient because killing doesn't affect the registers, so C-x r g a always inserts the good stuff.

like image 28
offby1 Avatar answered Oct 09 '22 00:10

offby1