0

I would like to connect broken lines, like:

This is a 
line that continues.

but instead of highlighting them with visual mode and connecting with Shift-J, I would like to run some kind of global command, like a substitution, which recognizes ends of lines in which the next line is not blank, and deletes the newline in-between, maybe adding a space if necessary. It might look like :%s/\n.//. Ideally, it would use the same formula that Shift-J uses. How would I do this?

1
  • gq} or gqG might do what you want. gq to format, and } motion to go to the end of the paragraph, or G to go to end of the file. This is not exactly the same as what you asked for since it doesn't "join" lines but rather reformats them, but it may cover your use-case. Sep 12, 2021 at 22:06

1 Answer 1

1

If your goal is to automate paragraph join I would approach it with the help of :global and :normal commands:

:g/^\S/normal! vipJ

That will convert whole buffer like

He was aware there were numerous wonders of this world including the
unexplained creations of humankind that showed the wonder of our
ingenuity. There are huge heads on Easter Island. There are the
Egyptian pyramids. There’s Stonehenge. But he now stood in front of a
newly discovered monument that simply didn't make any sense and he
wondered how he was ever going to be able to explain it.

The wave crashed and hit the sandcastle head-on. The sandcastle began
to melt under the waves force and as the wave receded, half the
sandcastle was gone. The next wave hit, not quite as strong, but still
managed to cover the remains of the sandcastle and take more of it
away. The third wave, a big one, crashed over the sandcastle completely
covering and engulfing it. When it receded, there was no trace the
sandcastle ever existed and hours of hard work disappeared forever.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.  Maecenas feugiat
fermentum pretium.  Cras eu dolor imperdiet justo mattis pulvinar.  Cras nec
lectus ligula.  Proin elementum luctus elit, a tincidunt quam facilisis non.
Nunc quis mauris non turpis finibus luctus.  Maecenas ante sapien, sagittis
quis accumsan in, feugiat quis sem.

to

He was aware there were numerous wonders of this world including the unexplained creations of humankind that showed the wonder of our ingenuity. There are huge heads on Easter Island. There are the Egyptian pyramids. There’s Stonehenge. But he now stood in front of a newly discovered monument that simply didn't make any sense and he wondered how he was ever going to be able to explain it.

The wave crashed and hit the sandcastle head-on. The sandcastle began to melt under the waves force and as the wave receded, half the sandcastle was gone. The next wave hit, not quite as strong, but still managed to cover the remains of the sandcastle and take more of it away. The third wave, a big one, crashed over the sandcastle completely covering and engulfing it. When it receded, there was no trace the sandcastle ever existed and hours of hard work disappeared forever.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.  Maecenas feugiat fermentum pretium.  Cras eu dolor imperdiet justo mattis pulvinar.  Cras nec lectus ligula.  Proin elementum luctus elit, a tincidunt quam facilisis non.  Nunc quis mauris non turpis finibus luctus.  Maecenas ante sapien, sagittis quis accumsan in, feugiat quis sem.

Note that visual selection is still activated with :normal but you can't see it.

Another way to do it would be macro recording or explicit ranges given to :join command.

4
  • Thanks. What does vipJ do? I am reformulating my vision a bit. I would like to join lines that are part of a continuous sentence, but not two consecutive lines that are not, for example two headers in a table of contents. It seems like "join" is the most standard Vim command, as you mentioned. So I'd need a condition to identify if two consecutive lines are part of a sentence or just two distinct, unpunctuated lines of text. It sounds pretty sophisticated. I'll think this over but I'd love to hear if you have any thoughts on this. Thanks very much Sep 13, 2021 at 14:21
  • I think to get off the ground I'll need to write a Vim function which evaluates line by line if this is a self-contained line such as a header or line of code vs if this is a broken natural language sentence. That will require me to think about regular expression features of these two syntactic types. Then it would be much easier to write a join function based on them. Sep 13, 2021 at 14:23
  • vipJ is a sequence of normal commands: vip visually select inner paragraph, J join visual selection.
    – Maxim Kim
    Sep 13, 2021 at 14:23
  • @JohnSmith clear understanding of requirements is at least a half of a solution. :)
    – Maxim Kim
    Sep 13, 2021 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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