質問

I've only recently learned about use v command to mark a section I want to copy or cut. This is great!

But I'd like to use the v command to make the section I want to replace.

For example:

$another_obj->method_in_obj
$self->method_actually_in_obj

I want to mark "another_obj" and overwrite "self" with "another_obj". Basically I want to tell vim that, I want to take these 4 characters (self) and turn them into these 11 characters (another_obj) and have vim adjust my line accordingly.

So the final should be:

$another_obj->method_in_obj
$another_obj->method_actually_in_obj

Thanks!

役に立ちましたか?

解決 2

  • cursor on [a]nother, pressing yw
  • then j cursor is on [s]elf, press:vawp

他のヒント

@Kent has outlined the basic approach: You yank the source word, then paste over the selected replacement. The downside is that you can do that only once, because that action overrides the original yanked text! To avoid that, you need to specify the black-hole register.

I do 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top