In Vim, what is the simplest way to join all lines in a file into a single line?
Asked Answered
D

6

70

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.

Dehumidifier answered 24/12, 2008 at 15:51 Comment(1)
See also "How to delete the '\n' of every line in a file".Glycogenesis
D
80

Ah, I found the answer.

:1,$join

Works like a charm.

EDIT: As pointed out in the comment:

:%join   -or-    :%j

...removes the range.

Dehumidifier answered 24/12, 2008 at 15:55 Comment(4)
This can also be written as: :%joinOrange
You may also want to use the gJ operation instead of j. The gJ operation joins the lines without inserting or removing any spaces.Ninepins
I self-answered this because I think this is much quicker than ggVGJ and slightly more elegant.Dehumidifier
Note: %j! will join without spaces. (Add an exclamation mark.) You can't use gJ with %.Aenneea
G
144

Another way:

ggVGJ

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

Glaudia answered 24/12, 2008 at 16:31 Comment(2)
Examples like this show why vim is so powerful. "gg", "V", "G", "J", are all serparate commands. excellentMonahan
As the accepted answer points out, :%j works, it's short, and doesn't require visual mode.Fallal
D
80

Ah, I found the answer.

:1,$join

Works like a charm.

EDIT: As pointed out in the comment:

:%join   -or-    :%j

...removes the range.

Dehumidifier answered 24/12, 2008 at 15:55 Comment(4)
This can also be written as: :%joinOrange
You may also want to use the gJ operation instead of j. The gJ operation joins the lines without inserting or removing any spaces.Ninepins
I self-answered this because I think this is much quicker than ggVGJ and slightly more elegant.Dehumidifier
Note: %j! will join without spaces. (Add an exclamation mark.) You can't use gJ with %.Aenneea
U
41

You can do it with 3 keystrokes starting from normal mode:

:%j
  • : 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.

Unmade answered 7/4, 2012 at 16:11 Comment(1)
:%j! gets rid of the space.Engaging
W
17

You can do it in three fewer keystrokes:

:1,$j

isn't ed grand?

Winterwinterbottom answered 24/12, 2008 at 17:0 Comment(0)
C
11

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

Contexture answered 24/12, 2008 at 22:36 Comment(0)
O
9

Cryptic way:

qqqqqJ@qq@q

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

Orange answered 24/12, 2008 at 16:55 Comment(3)
Of course. :-p reddit.com/r/programming/comments/61no8/…Orange
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.Weighty
@Weighty : Because if you have something in the q register, you will execute that macro while recording 'J@q'.Fredrickafredrickson

© 2022 - 2024 — McMap. All rights reserved.