Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove single line breaks but keep empty lines with Vim

Is it possible to remove single line breaks but to keep empty lines (double line breaks I guess?) in Vim? I'm using Vim's HardPencil plugin to do some word processing, and I like the hard line breaks because it makes it easier to bounce around the file. However, I need to share this information in LibreOffice Writer or Word, and hard line breaks won't work well if others edit the file. To clarify, this is what I have:

Line1.
Line2.
Line3.

Line4.
Line5.

And this is what I would like:

Line1. Line2. Line3.

Line4. Line5.

Essentially hard line breaks should be replaced with a space, and double line breaks should be preserved.

I've tried variations of :%s/\n but can't get the double line breaks to be preserved. I've found similar questions here and here, but these are not Vim specific.

like image 866
haff Avatar asked Dec 25 '22 05:12

haff


1 Answers

You can replace line+line break with just line, and use negative look ahead \(\n\)\@! to make sure it's not followed by an empty line:

:%s/\(.\+\)\n\(\n\)\@!/\1 /

@Peter_Rincker's cleaner solution:

:%s/.\+\zs\n\ze./ /
like image 100
Fabricator Avatar answered Jan 28 '23 09:01

Fabricator