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?

| improve this question | | | | |
  • Just note that with a word processing software, you can usually make paragraph breaks to have a larger spacing, instead of having two line breaks to "pretend" to be paragraph breaks. – Alvin Wong Feb 3 '13 at 15:11
  • The closest I come is :%s/\n\{2,}/\rSomemarkerNotFoundElsewhereInTheText\r/e to replace all double newlines with a marker, followed by :%j to join all line and %s/SomemarkerNotFoundElsewhereInTheText/^M^M/g to replace the marker. Ugly as hell and I am sure there is a better way. – Hennes Feb 3 '13 at 15:14

Something like this


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.

| improve this answer | | | | |
  • Should that be \&\& on the last line? – alexis Feb 3 '13 at 15:09
  • Typo, I'm sorry. – romainl Feb 3 '13 at 15:39
  • I don't know vim's REs, but if they're perl-style, \w will not match lines that end with punctuation. I think you want \S, which matches any non-space character. – alexis Feb 3 '13 at 17:21
  • Good point, @alexis. – romainl Feb 3 '13 at 18:13

You can use VipJ on each paragraph:

  • V: start linewise selection.
  • ip: select entire paragraph.
  • J: joins lines together.

Because you separate each paragraph with a double newline, we can record a macro to do this for us automatically:


To join N paragraphs together, simply use N@q now.

| improve this answer | | | | |
  • The only problem with this is that one line paragraphs are joined with the following paragraph. – Moyamo Feb 23 '13 at 6:37

There are a number of ways to do this in Vim, but I think the simplest is this:

:set tw=9999

That moves the cursor to the first line of the buffer (gg) and reformats (gq) all the lines from there to the last line (G). See

:help gq
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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