Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

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

share|improve this answer
    
Thanks. This is getting very close for me, however, it's deleting the blank lines between paragraphs. (I tried fileformat unix and dos to see if it was a Windows thing). Also, could you translate the commands a bit? G goes to the last line. :a according to help is "append" but I'm not sure what that does. Also the g/pattern/cmd you have there has lost me. What's the command? –  dggoldst Oct 18 '10 at 13:59
    
@dggoldst: I've changed the command to retain empty lines. The first three lines are just to add an empty line at the end of the file (otherwise the last paragraph won't be matched). The last command is :g, which applies a command to all matching lines; see :help :g and perhaps this thread on how :g behaves when you delete line breaks. –  Gilles Oct 18 '10 at 14:45
add comment

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"
share|improve this answer
    
This works for me. Would be good to add a "gg" at the beginning to put the cursor at the top of the file. Also, using J instead of gJ doesn't cause separate words to mash together into one. This might only be the case for my values of "formatoptions", which are currently puzzling me. –  dggoldst Oct 18 '10 at 14:20
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.