Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste to a particular line in vim without moving the cursor

Tags:

vim

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

like image 224
user3667089 Avatar asked Nov 23 '15 23:11

user3667089


2 Answers

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.

like image 170
Bram Avatar answered Oct 19 '22 23:10

Bram


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.

like image 30
Kent Avatar answered Oct 19 '22 23:10

Kent