Take the 2-minute tour ×

I want to join all lines in a file into a single line. What is the simplest way of doing this? I've had poor luck trying to use substitution (\r\n or \n doesn't seem to get picked up correctly in the case of s/\r\n// on Windows). Using J in a range expression doesn't seem to work either (probably because the range is no longer in 'sync' after the first command is executed).

I tried :1,$norm! J but this only did half of the file - which makes sense because it just joins each line once.

share|improve this question
See also "How to delete the '\n' of every line in a file". –  ib. Apr 8 '12 at 2:18

7 Answers 7

Another way:


"ggVG" visually selects all lines, and "J" joins them.

share|improve this answer
If i could upvote you I would. Examples like this show why vim is so powerful. "gg", "V", "G", "J", are all serparate commands. excellent –  theman_on_vista Dec 24 '08 at 16:41
why can't you upvote this? –  Nathan Fellman Dec 24 '08 at 20:49
i am not really registered with the site –  theman_on_vista Dec 26 '08 at 15:44
and there is a rep requirement for voting. –  R. Martinho Fernandes Jan 6 '09 at 5:26
awesome answer! –  user210757 Apr 13 '12 at 16:52
up vote 44 down vote accepted

Ah, I found the answer.


Works like a charm.

EDIT: As pointed out in the comment:

:%join   -or-    :%j

...removes the range.

share|improve this answer
This can also be written as: :%join –  Josh Lee Dec 24 '08 at 17:00
Or abbreviated as :%j –  Judge Maygarden Dec 24 '08 at 17:26
You may also want to use the gJ operation instead of j. The gJ operation joins the lines without inserting or removing any spaces. –  Kris Kumler Dec 24 '08 at 21:38
I self-answered this because I think this is much quicker than ggVGJ and slightly more elegant. –  Jordan Parmer Jan 6 '09 at 23:44
Note: %j! will join without spaces. (Add an exclamation mark.) You can't use gJ with %. –  Rich Sep 5 '09 at 19:03

You can do it with 3 keystrokes starting from normal mode


: enters command mode % refers to all lines in the file j executes the join command

Now it seems that this adds a space between the lines. I am not sure if you want this.

share|improve this answer
+1, this is the most simple one I found. –  zdd May 23 at 6:31

You can do it in three fewer keystrokes:


isn't ed grand?

share|improve this answer
:%j is 5 fewer... –  Judge Maygarden Dec 24 '08 at 17:27

I’m surprised no one even mentioned the other way:

:%s/\n/ /

I am equally surprised that no one pointed out that the range 1,$ has a shorthand that’s written %.

(This doesn’t do the same thing as joining the lines, but depending on circumstances that may in fact be more appropriate.)

share|improve this answer

Cryptic way:


(the first three q's clear the q register, the qqJ@qq records a macro to the q register that performs a Join, then calls q, and the last @q runs it.

share|improve this answer
Of course. :-p reddit.com/r/programming/comments/61no8/… –  Josh Lee Dec 25 '08 at 7:36
Why would you want to clear the q register first, when you overwrite it anyway. That's like doing a bunch of no-ops to make your command longer. –  Alf Jan 7 '09 at 8:25
@Alf : Because if you have something in the q register, you will execute that macro while recording 'J@q'. –  Michele Gargiulo Feb 2 '12 at 15:01

I would have done 10000P

EDIT: I meant 10000J

share|improve this answer
What would you be pasting 10000 times? –  Judge Maygarden Dec 24 '08 at 17:25
Err, been a bit long. I meant 10000J –  Joshua Dec 24 '08 at 20:43
I see what your are saying, but that is kind of like watering a flower pot with a fire hose. –  Jordan Parmer Jan 4 '09 at 22:01
Kudos... get er done! –  ojblass Mar 25 '09 at 6:11

Your Answer


