VIM Replace word with contents of paste buffer?

后端 未结 9 1410
灰色年华
灰色年华 2020-12-22 15:41

I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g. I know that this is the typical way

相关标签:
9条回答
  • 2020-12-22 16:14

    You can use registers for that:

    first place replacement text in register <mark some text>"ay

    where a is register name

    then you can use that register in replacement

    ve"ap

    0 讨论(0)
  • 2020-12-22 16:15

    If your cursor is on the word you want to replace with the contents of the unnamed register, you can use viwp. v switches to visual mode, iw selects the inner word, and p puts the contents of the register in its place.

    In practice, when I need to replace one word (function name, etc.) with another, I'll move to the one to use as a replacement, yiw to yank the inner word to the unnamed register, then move to the word I'm replacing, and viwp to replace it. Pretty quick way of substituting one word for another. If you searched (/) for the word you're replacing to get to it, you can then just hit n to get to the next occurrence you need to replace. Obviously no substitute for using :%s/find/replace/g, but for a couple of quick substitutions it can be handy, especially if you already have the new word in a register.

    0 讨论(0)
  • 2020-12-22 16:15

    If you make use of a named register (ie. use "ay or "ad, etc., to fill your paste register), you can do something like

    cw<CTRL-R>a<esc>

    Which will replace the word with the contents of register a. As far as I can tell, you can't use the default register because when you cw it'll be filled with the word that was cut by that command.

    0 讨论(0)
提交回复
热议问题