Tutorial :Vim duplicate selection



Question:

If i have something selected in VIM in visual mode .... how can i duplicate that selection and place it below or above the selection.


Solution:1

Just hit 'y' to yank what you've got selected visually, the "p" to paste below the cursor or "P" to paste above.

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


Solution:2

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.


Solution:3

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 your doing this a lot


Solution:4

use y for yank (copy) the selection into a buffer

the p for paste the selection where you want it to be


Solution:5

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

yy

to yank the whole line, then

p

to paste.


Solution:6

You have two options

  • yy which copies current line, then p to paste
  • make selection (with v for example), then copy with y and paste with p

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »