Mad Wombat
Mad Wombat

Reputation: 15155

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

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?

Upvotes: 47

Views: 12839

Answers (8)

JJ on SE
JJ on SE

Reputation: 417

If you don't care to preserve the highlighted text in the kill ring you could use something similar to the answer above by ekneiling

(defun replace-yank(beg end)
  (interactive "r")
  (delete-region beg end)
  (yank 1))

(global-set-key (kbd "C-S-y") 'replace-yank)

One advantage is the above can be done repeatedly.

Upvotes: 0

ekneiling
ekneiling

Reputation: 157

For anyone landing on this who doesn't want to change the global setting like me, I use this function and key binding:

(defun replace-yank(beg end)
  (interactive "r")
  (kill-region beg end)
  (yank 3))

(global-set-key (kbd "C-S-y") 'replace-yank)

I intentionally add the selected text to the kill ring incase I want it later, and yank what was previously first in the ring.

Upvotes: 3

offby1
offby1

Reputation: 7023

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.

Upvotes: 17

Drew
Drew

Reputation: 30708

Use delete-selection-mode, so that pasted text replaces the active region.

Use the secondary selection, to paste the same text over and over, even if you alternately select a new region to replace.

See http://www.emacswiki.org/emacs/SecondarySelection.

Upvotes: 2

Singletoned
Singletoned

Reputation: 5129

The way I do this is:

  • go to where you want the new stuff
  • paste the good stuff
  • your cursor is now between the new stuff and the stuff you want to get rid of
  • select forward until everything you want to get rid of is selected
  • delete it

It's a slightly different way of thinking about it. Paste what you want, then get rid of what you don't want, rather than replace what you don't want with what you do.

Upvotes: 4

user310031
user310031

Reputation: 61

If you enable CUA-mode, this paste over selected region becomes the normal behaviour.

Upvotes: 3

BobS
BobS

Reputation: 2648

The default way to do something like this is the not-entirely-elegant following:

  1. Get your desired replacement text into the kill ring somehow (e.g., M-w).
  2. Highlight the region to be replaced.
  3. Delete it (C-w).
  4. Replace it with the prior-copied region (C-y, M-y). This replaces the freshly deleted contents with the exact same text that you just deleted (C-y), and then re-replaces it with the next most-recently saved buffer in the buffer ring (M-y).

This would get to be a real pain if you wanted to do this 10 times with the same text, as the desired replacement would get pushed farther back in the kill ring each time you deleted a region, so you'd have to call M-w an increasing number of times each time you wanted to yank it.

I've also just discovered M-x delete-region, thanks to Emacs: how to delete text without kill ring?. As the question implies, this deletes the offending text without putting it into the kill ring, avoiding the problem of pushing your replacement text further down on the stack. And, as the relevant response mentions, you can bind this to a shortcut key of your choosing.

Upvotes: 9

Michael Mrozek
Michael Mrozek

Reputation: 175695

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

Upvotes: 84

Related Questions