Sato
Sato

Reputation: 8582

How to copy multiple lines one under each line separately?

Sorry for the bad english, see this example:

original

a
line01
line02
line03
line04
b

want to become:

a
line01
line01
line02
line02
line03
line03
line04
line04
b

a and b are irrelevant.

I can copy one line and paste, and repeat. Is there any simple solution? like one command?

Upvotes: 3

Views: 287

Answers (3)

Peter Rincker
Peter Rincker

Reputation: 45087

For funsies a sed solution:

$ sed 'p' input.txt > output.txt

Using filter, :!, inside of Vim:

:%!sed p

Obligatory awk solution: awk '1;1' input.txt > output.txt.

Upvotes: 0

Max Friederichs
Max Friederichs

Reputation: 599

You could also write a vim macro-

With your cursor at line 0, column 0; record a macro, store in register a

qa

Copy the current line; paste it below; move your cursor down to the next line

yypj

Save the Macro

q

Now run the a macro N number of times (it will stop at the bottom of the file regardless)

3@q

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

Using a global command, this could easily be done like this

:g/^/t.

Breakdown

:g              start a global command
/^              search for a begin of line (every line matches)
/t.             copy the current line

Upvotes: 4

Related Questions