Reputation: 8109
I can't find out how to join all lines till a next condition happens (a line with only 1 or more numbers) p.e.
input:
1
text text text text (with numbers)
text text text text (with numbers)
2
this text
text text text text (with numbers)
text text text
3
text text text text (with numbers)
4
etc
desidered output:
1 text text text text (with numbers) text text text text (with numbers)
2 this text text text text text (with numbers) text text text
3 text text text text (with numbers)
4
etc
I normally use global/^/,+2 join
but the number of lines to join are not always 3 in my example above.
Upvotes: 6
Views: 770
Reputation: 8905
Just because of the comment by Tim that it couldn't be done with only a regular expression search and replace using Vim, I present this: how to do it with only a regular expression search and replace, using Vim:
:%s#\s*\n\(\d\+\s*\n\)\@!# #
If you're not fond of backslashes, it can be simplified using "very magic" \v
:
:%s#\v\s*\n(\d+\s*\n)@!# #
This is adapted from Tim's Perl-style regular expression given in the same comment, improved to make sure the "stop line" only has numbers (and maybe trailing whitespace).
See :help perl-patterns
if you're comfortable with Perl and find yourself having trouble with the Vim regular expression dialect.
Upvotes: 3
Reputation: 172510
Instead of the static +2
end of the range for the :join
command, just specify a search range for the next line that only contains a number (/^\d\+$/
), and then join until the line before (-1
):
:global/^/,/^\d\+$/-1 join
Upvotes: 6
Reputation: 22596
v/^\d\+/-j
will do the trick.
v
execute the function for each not matching the condition
^\d\+
your condition : Line starting with a number.
-j
go one line backward an join. Or if you prefer join the current line with the previous line.
So basically we join every lines not matching your condition with the previous line.
Upvotes: 5