Thursday, April 28, 2011

Vim duplicate 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.

From stackoverflow
  • 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.

    Paul Tomblin : a) he didn't say it was a single line, and b) why get out of visual mode if you've already got it selected?
  • use y for yank (copy) the selection into a buffer

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

    solomongaby : using p for paste breakes the formating because it doesnt take the whole lines
  • 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.

    solomongaby : thanks that was what i was looking for. Do you know how i could paste it below ?
  • 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
    solomongaby : using p for paste breakes the formating because it doesnt take the whole lines
  • 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

    solomongaby : that doesn't seem to work, do you press ' and then SHIFT+. to get > ?
    Paul Tomblin : It works for me.
    solomongaby : i am actualy using a VIM plugin for Netbeans so this might be the problem
    michael : '> has 'not in vi' in the vim help so you'll be missing out on that one. :helpgrep not\ in\ Vi yields 709 matches in the vim help. So you probably shouldn't confuse the two. Most emulators only cover the basics

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.