Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Pasting from Visual mode

#54

Run time:

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.

Shownotes

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

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..