Join lines inside paragraphs in vim

Suppose you've typed a long document in vim with automatic line breaking on, so all the lines have been broken at, say, 79 characters. You may have even applied formatting to the whole document to break all the lines at that length.

Paragraphs are demarcated in your document by blank lines.

Now you decide you don't want line breaks within paragraphs at all.

How can you remove all the line breaks within paragraphs without eliminating the paragraph boundaries altogether?

I have made this quick and idiotic hack, but am looking for the proper way.

:%s/^\s*$/@@@@@ - replace blank lines with @@@@@
ggVGgJ           - join all lines in the file
:%s/@@@@@/\r\r/g   - replace @@@@@ with line breaks

I think this does what you want: make sure there is an empty line at the end of the file, then join every paragraph (terminated by an empty line).

G:a

.
:g/^./ .,/^$/-1 join

Explanation: first go to the end of the file and append an extra empty line with :a (maybe there's a more elegant way to do that; interactively, you can replace the first three lines with o<ESC>). Then, for every non-blank line that hasn't been considered yet (:g/^./), apply the join command to the range starting at the selected line (.) and ending one line before the next empty line (/^$/-1).

Optionally, :g/^$/d if you don't want any blank line to remain (then you can take off the -1).


This should do it:

:set tw=99999
gggqG

tw is set to some value at least as large as the number of characters in the longest paragraph. gg moves the cursor to the first line; gq is the command to reformat; G moves the cursor to the last line, telling gq to reformat from the current cursor location to the last line.


  1. Set the cursor inside the desired paragraph

  2. Type: vipJ

(vip highlights the current paragraph, J joins all lines)


Might not be the cleanest way to do it, but here's what I would use:

ggqav}bgJ}wq999@a   # go to the top of the file (gg)
                    # start recording macro "a" (qa)
                    # select the entire paragraph (v})
                    # go back one word so as not to join the blank line (b)
                    # join the selected lines without spaces (gJ)
                    # go to first word of the next paragraph (}w)
                    # finish recording the macro (q)
                    # 999 times (arbitrary number of paragraphs to join)
                    # run macro "a" (@a)

(Works in VIM 7.2.)

Depending on exactly how the paragraphs were split when they were originally formatted, you may want to replace the gJ command (join without spaces) with the J command (join with spaces).

The arbitrary 999 count only needs to be at least the number of paragraphs in the file -- you can select a larger number if you want and lose nothing but processor cycles. Assuming one and only one blank line between paragraphs, you can get a more accurate paragraph count using:

:%s/^$//gn     # returns "999 matches on 999 lines"