Join lines between a certain text pattern in Vim

I have a text file which looks like this:

A.
text
text
text


A.
more text

more text

A.
more text

I want to join all lines between the "markers" A. so that it looks like:

A.texttexttext
A.more textmore text
A.more text

How can I do this in Vim?


Solution 1:

:%s/\n\(\(A\.$\)\@!.*\)/\1/

Substitute a pattern matching:

  1. newline,
  2. a group containing of

    1. not the string A. directly followed by end-of-line, then
    2. any character until end of line

with:

  • everything matched except the starting newline (i.e. the group above),

and do this globally.

Solution 2:

This also works (when the first line starts with A.)

:v/^A/-1j!