sponsor Vim development Vim logo Vim Book Ad

basic Tip #1463: copy multiple lines/words to a specified position

 tip karma   Rating 5/10, Viewed by 2422 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 9, 2007 23:12      complexity:   basic
author:   Vincent Wang (linsong DOT qizi AT gmail DOT com)      as of Vim:   5.7

Sometimes, I often copy manlines/words from different places to a specified position, for example:
    1.  foo
    2.  bar
    3.  blah
    4.  aaaaaaaaaaaa
    5.  bbbbbbbbbbbb
    6.  ccccccccccccc
I want to copy line 1, 3, 4 under line 6:
    1.  bar
    2.  bbbbbbbbbbbb
    3.  ccccccccccccc
    4.  foo
    5.  blah
    6.  aaaaaaaaaaaa

I worked out  the following map to make it eaiser :
    :vmap gy y:call CopyToLastEditPos()<CR>

    function CopyToLastEditPos()
        let cmd = 'gi<C-O>gp'
        if visualmode()==# 'V'
            let cmd = cmd . '<C-O>k<C-E>'
        endif
        let cmd = cmd . '^[`>'
        :exec 'norm ' . cmd
    endfunction

then first, you need to enter insert mode under line 6, then return to normal mode, go and select lines seperately that need to be copied in visual mode, then type "gy", the line will be copied to under line 6.  Hope it can save your time. Thanks!

NOTE: you need to press Control-v Control-o to input <C-O>, press Control-v Control-e to input <C-E>, press Control-v Esc to input ^[

 rate this tip  Life Changing Helpful Unfulfilling 

<< motion on steroids | Notes to accompany : "Introduction to display Editing using vi" paper. >>

Additional Notes

Karthick, January 9, 2007 23:53
You can use any of the upper case registers to yank the lines. Yanks to upper-case registers append (and dont overwrite) existing contents.
Anonymous, January 10, 2007 0:55
yes, we can use capital letter register to do this. But that method needs to press more keys. The above solution is a bit neat. And you can see what is copied on the way. Thanks.
info at bertram dash scharpf dot de, January 10, 2007 5:35
Ever thought of ":m'}-1"?

In GVim I mapped something like

  :map <S-up>   :m-2<cr>
  :map <S-down> :m+1<cr>
lpenz, January 10, 2007 5:55
> But that method needs to press more keys.

Yeah, people say that. But Vim has macros that are very easy and fast to record. And I, for instance, have @q, @w, @e, @r bound to F5, F6, F7 and F8. This way I can always record a macro and replay it as many times as I want quickly, solving the "this needs more keys" problem.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo