Vim undoing text width formatting

formattinglibreofficenewlinesvim

I wrote a document in vim with textwidth=80. I would now like to paste this document into libreoffice for formatting. The problem is the line breaks. In libreoffice the lines are too short because of the newlines inserted by vim after 80 characters.

I separate paragraphs using two newline characters (i.e. hitting return twice). Is there any way I could remove all the single \n characters while retaining the \n\n characters?

Best Answer

Something like this

:%s/\(\S\)\n/\1
:%s/\n/&&

should work. Well, it does here.

The first substitution matches a "non-whitespace character" (it could be more specific) followed by a newline, capturing that "non-whitespace character" for use in the replacement (\1). Practically, it turns every "paragraph" into a single line.

The second one substitutes every newline character with two newline characters. & is used in the replacement to mean "the matched text" so, here && means "two newline characters". It could be written :%s/\n/\r\r but && is shorter.

Related Question