Subscribe for free with iTunes, Twitter, or RSS (Ogg or Quicktime).

Vimcasts

Pasting from Visual mode

#54

When used in Visual mode the p command replaces the selection with the contents of a register. This makes for a smooth workflow when you want to overwrite a selection, or swap the order of two regions of text.

Download

OGG 3.8 MB

Quicktime 6.5 MB

From Vim’s documentation (see :help v_p):

When using a put command like p or P 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