-1

I have the lastest versions of arch and nvim which I am using.

I have a text that looks like this

the slow brown  
fox jumped  
over a   
bridge  

the blind brown  
fox stumbled  
upon a   
badger
  

the quick brown  
fox jumped  
over a   
lazy dog  

the quick brown  
fox jumped  
over a  
cow    

Each line ends with a \n.

There are 100's of paragraphs. Each paragraph is unique.

I want it to look like this:

the slow brown fox jumped over a bridge  

the blind brown fox stumbled upon a badger
  
the quick brown fox jumped over a lazy dog  

the quick brown fox jumped over a cow  

.. etc

Without using n J hundereds of times how can I automate this with a command?

4
1

I've answered this before, but the general strategy is this: globally join from the start of a paragraph to the end (given by /^$/-). But we have to handle the last line of the document specially. The easiest version is

:$put =''
:global/./,/^$/-join
:$delete

where we make a blank line at the end, do our join (which doesn't fail on the last line, now), and then remove the spurious blank line at the end. Takes about 30s interactively, or less if you abbreviate:

:$pu ='' | g/./,/^$/-j
:$d

(because :global eats | for its command, the delete has to be done separately, or you have to use :execute, which I think is definitely slower to type.)

1
  • I've now a feeling I asked this before somewhere, possibly here. Apologies. – Kes Feb 24 at 22:01

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