Saideep Pavuluri
Saideep Pavuluri

Reputation: 102

Copy selected text n times using vim editor

How is it possible to copy multiple lines and paste them 'n' number of times using vi editor.

For example:

This is the first line.
This is the second line.
This is the third line.

Now I would like to copy the 3 lines as they are 100 times one after the other like:

#First copy
This is the first line.
This is the second line.
This is the third line.
#Second copy
This is the first line.
This is the second line.
This is the third line.
...
...

Upvotes: 0

Views: 3109

Answers (2)

sudo bangbang
sudo bangbang

Reputation: 28189

Vim has a concept called count that helps to iterate a lot of commands.

In this case, yanking and pasting can be used with count.

3y will yank 3 lines.
100p will paste those lines 100 times.

run :h count to learn more

An optional number that may precede the command to multiply
or iterate the command. If no number is given, a count of one
is used, unless otherwise noted.

Upvotes: 3

romainl
romainl

Reputation: 196546

Assuming you want to copy those three consecutive lines:

This is the first line.
This is the second line.
This is the third line.

and paste them three-by-three like this:

This is the first line.
This is the second line.
This is the third line.
This is the first line.
This is the second line.
This is the third line.
This is the first line.
This is the second line.
This is the third line.
...

and your cursor is on the first line, you can achieve your goal with a mix of Ex commands and normal mode commands like this:

3:y<CR>jj99p

which expands to:

:.,.+2y<CR>jj99p

or like this with a visual-line selection:

Vjjy99p

or like this if you know the line numbers:

:1,3y|3|normal! 99p<CR>

or like this if you want to use the "paragraph" text-object and motion:

yip}99p

or simply:

3yjj99p

And I'm sure you could find a dozen more ways.

Upvotes: 1

Related Questions