5
votes

Lets say the cursor is at line N and we yank it with yy and we want to paste to 20 lines below the cursor is. Is it possible to do so without moving the cursor down like 20j, p, 20k? Tried 20p but it just pasted 20 duplicate lines. I can't find a solution after some googling, and I can't be the only one that need this.

What I want to do is yank (yy) the current line, paste to 20 lines below, go down one line (j), yank (yy) the current line, paste to 33 lines below, go down one line (j), yank (yy) the current line, paste to 41 lines below, go down one line (j), rinse and repeat

3
the 20 you have to type anyway, and p too, so you are just fighting with one single keystroke? 20jp is fine, isn't it? if you want to go back to the line you yanked, just press ``Kent
`` is better than 20k but I was hoping there is something betteruser3667089
do you really need a mapping? say press for example 20<someKey> then vim does paste without cursor moving?Kent
@Kent If the only way to achieve this is to create a custom mapping, I will accept your `` if you write it as an answer.user3667089

3 Answers

7
votes

You can use the :co[py] command:

:[range]co[py] {address}                   :co :copy
     Copy the lines given by [range] to below the line
     given by {address}.

So for the example given: :co .+20 would copy the current line 20 lines down.

5
votes

If you are looking for a built-in command/option for pP, there is no one. You can always press `` to go back your old position.

If you want to create custom mapping, there are many ways. E.g. write a function, write a custom command etc.

If you just want to automate some actions, like, for each line between 2-20, duplicate and copy to 5 lines below current line. you can consider to use :g command.

3
votes

I can't believe that this question has been here for 5 years and nobody has actually given the right answer...

What you're looking for is the ex command :put (https://vimhelp.org/change.txt.html#%3Aput).

From the documentation:

:[line]pu[t] [x]
Put the text [from register x] after [line] (default current line).

So with your example, the steps would be:

yy
:+20put

Also see this reddit post, I found it quite helpful: https://www.reddit.com/r/vim/comments/p7blsn/question_how_to_paste_register_to_a_specific_line/