Replace word with what's ready to put in vi/vim?

China☆狼群 提交于 2019-12-07 02:49:15

问题


For example, I have:

rm -f $(NAME).elf $(name).d

where the second 'name' is a typo, and should also be 'NAME'.

So on 'N', I did yw, and then moved to 'n'.

I realised that if I now hit cw to change it, I'll be in insert mode - but I thought ah well, it's only one extra key (ESC) and then I can just p the corrected version in.

But in that case, p did exactly what u would have done, it put 'name' back. I guess because the 'old text' is loaded into the register on change too, which I didn't realise.


Is there a "replace with register" command that can be applied to a word/letter/etc.?


回答1:


after you yw on NAME and moved to name you can do:

viwp

or golf abit: vep




回答2:


I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Your example

With NAME yanked into the default register, and the cursor on the n, just use gre. gr invokes the command, and e is the motion that covers the entire name to be replaced.




回答3:


Yet another way:

ciw<Ctrl-r>0

or

ce<Ctrl-r>0

This would allow for repeating with . without the use of any plugin, as you mentioned on the comments that you are currently avoiding them.




回答4:


You could simply change the case of name with gUiw.



来源:https://stackoverflow.com/questions/25037482/replace-word-with-whats-ready-to-put-in-vi-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!