arunmoezhi
arunmoezhi

Reputation: 3200

Duplicate each line in VI

I have a file with these lines:

aa
bb
cc
dd

I want to convert this into:

aa
aa
bb
bb
cc
cc
dd
dd

Is this possible in VI?

Upvotes: 30

Views: 18976

Answers (3)

kev
kev

Reputation: 161674

Try this simple one:

:g/^/norm yyp

Yet another one(shorter):

:%s/.*/&\r&

Another one:

:%!sed p

Upvotes: 56

dimitriy
dimitriy

Reputation: 9460

I like g/^/t.
The g (for global) command will look for any lines that match a pattern.
The pattern we specified is ^, which will match all lines.
t will copy and paste, and finally
the dot tells it to paste below.

Do I win for brevity?

Upvotes: 45

pb2q
pb2q

Reputation: 59617

Use the global command g to operate on every line in the file:

:g/^/norm yyp

The g command will operate on all lines that match a pattern. ^ is a pattern which will match any line. norm executes the command yyp, which yanks the current line, and pastes it. :g/^/norm Yp will also work.

See :help global for more details about the command, and see also this vim wiki page on g.

Upvotes: 12

Related Questions