From Vim’s documentation (see :help v_p
):
When using a put command like
p
orP
in Visual mode, Vim will try to replace the selected text with the contents of the register.
This command has a side-effect: the text that was originally selected is copied to the default register. In effect, this means that the text in the default register and the text that’s selected in the document swap places.
replace{motion} commands
In general, I encourage staying out of visual mode where possible. For example, instead of visually selecting a word then deleting it (viwd
), I prefer to use the d{motion}
operation (diw
). But Vim has no built-in Normal mode operation that emulates the behaviour of put in Visual mode, so this is one place where I encourage using Visual mode.
If you’d like to make this operation without using Visual mode, there are several plugins that add this behavior.
Kana Natsuno’s operator-replace plugin doesn’t provide a mapping by default, but in the documentation the author suggests binding the operation to _{motion}
. Ingo Karkat’s ReplaceWithRegister plugin creates a gr{motion}
mapping. Steve Vermeulen’s easyclip plugin provides a substitution operator.
If I’ve missed any plugins that provide this functionality, please leave a comment below.
Further reading
:h v_p
:h quote0
:h quotequote
- Practical Vim, tip 61: Replace a visual selection with a register