30

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?

2
  • 1
    Probably of interest for many people getting here : stackoverflow.com/q/29368158/812102 Commented Apr 10, 2016 at 14:47
  • @MikaAndrianarijaona and what do you propose to use when it turns out that Atom can't do something we want? Switch to VSCode? Commented May 2, 2024 at 8:44

3 Answers 3

56

Try this simple one:

:g/^/norm yyp 

Yet another one(shorter):

:%s/.*/&\r& 

Another one:

:%!sed p 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the s option, because I was looking to do this for a visual selection and that worked for me.
45

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?

2 Comments

I like the g/^/norm yyp as it could very simply duplicate or triplicate .. or even more by just adding p as needed ... g/^/norm yypp :) 10x
i liked the accepted answer, more, but upvoted this one, instead, because of the (even small but thoughtful) effort to explain. Thank you.
12

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.

1 Comment

@Conner: That doesn't work, as % iterates over the original range, but the command adds lines. You need the "mark first, then iterate" logic of :global here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.