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

后端 未结 7 2068
一向
一向 2020-12-24 05:26

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 on

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 06:04

    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.

提交回复
热议问题