The Computer Oracle

Join lines between a certain text pattern in Vim

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4

--

Chapters
00:00 Join Lines Between A Certain Text Pattern In Vim
00:23 Accepted Answer Score 5
00:49 Answer 2 Score 8
00:59 Answer 3 Score 1
01:36 Thank you

--

Full question
https://superuser.com/questions/420497/j...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#vim

#avk47



ANSWER 1

Score 8


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

:v/^A/-1j!



ACCEPTED ANSWER

Score 5


:%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.




ANSWER 3

Score 1


A more general solution that also works with more more complex input

:$s/$/A./ | g/A./,/A./- s/\n\(A\.\)\@!//
:$s/A\.$//
  1. insert missing A. at end of file
  2. Delete all newlines in between tags A.
  3. Delete the closing A. tag at the end of file
    *I Had to run this in e separate command, otherwise vim executed it before it had removed newlines. Can probably be solved somehow.

Input

B.
Don't join


A.
text
text
text


A.
more text

more text

A.
more text

Output

B.
Don't join


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