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.

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?

share|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
add comment

3 Answers

up vote 0 down vote accepted

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.

share|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
add comment

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

:set tw=9999
gggqG

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

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:

qqVipJ2jq

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

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