Goran Jovic
Goran Jovic

Reputation: 9508

How to most easily duplicate a piece of text in Vim?

Let's say I have a piece of text within my code which I want duplicated.

What I've been doing so far is to move to the start, press v to enter visual mode, then move to the end, press y to yank the text, move one character back and then p to put it there.

Is there an easier way? Something like:

  1. Select text with v.
  2. Press some sequence of commands and there is - it's duplicated.

Upvotes: 1

Views: 216

Answers (2)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

You can always define custom macros / shortcuts on the fly.

map ! "+yP

will allow you to enter visual with v, mark the text, and use ! to duplicate it once (for extra duplicates, repeat p or P as needed.

Upvotes: 2

Mat
Mat

Reputation: 206659

If it's the "move one char back" you're trying to avoid, press P rather than p for the "paste" operation.

Upvotes: 6

Related Questions