Gabriel Solomon
Gabriel Solomon

Reputation: 30105

How to duplicate a selection and place it above or below the selection

If I have something selected in Vim in visual mode, how can I duplicate that selection and place it below or above the selection?

Upvotes: 17

Views: 9701

Answers (6)

Adam Neal
Adam Neal

Reputation: 2177

Do you want to copy/paste the whole line? If so, get out of visual mode, then use yy to yank the whole line, then p to paste.

Upvotes: 0

Eric
Eric

Reputation: 19873

Use y to yank (copy) the selection into a buffer.

Use p to paste the selection where you want it to be.

Upvotes: 1

Jakub Arnold
Jakub Arnold

Reputation: 87260

You have two options:

  • yy which copies the current line, then p to paste.
  • Make a selection, with v for example, then copy with y and paste with p.

Upvotes: 0

michael
michael

Reputation: 11847

In addition to the V...yp combo you might want to know about some jumps '< and '> to get to the last character of the previous visual mode text. Specifically, if you want to paste below you'd go V...y'>p. If it's a long multiline it may be handy.

It's one of those jumps you may find handy if you're doing this a lot.

Upvotes: 8

Paul Tomblin
Paul Tomblin

Reputation: 182880

Press y to yank what you've got selected visually, then p to paste below the cursor or P to paste above it.

And since you asked about pasting below the selection block, I'll copy what michael said below: After you y to yank, use '> to move it to after the selection block, and then p to paste.

Upvotes: 33

Woahdae
Woahdae

Reputation: 5051

Since I do this a lot (select a block, yank, go to end of last visual selection, paste) I set up a visual block shortcut under CTRL+P (prior to this, CTRL+p seems to be the same as j in a visual block).

vmap <C-p> y'>p

Now it's just making a visual selection and pressing CTRL+p.

Upvotes: 11

Related Questions