简体   繁体   中英

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

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.

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.

3 y will yank 3 lines.
1 0 0 p 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM