Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join all lines till next condition?

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.

like image 211
Reman Avatar asked May 27 '14 14:05

Reman


People also ask

How do you join all lines in Python?

Python String join() The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.

How do you join multiple lines in Python?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.

How do I merge lines in Vim?

When you want to merge two lines into one, position the cursor anywhere on the first line, and press J to join the two lines. J joins the line the cursor is on with the line below. Repeat the last command ( J ) with the . to join the next line with the current line.


2 Answers

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
like image 184
Ingo Karkat Avatar answered Sep 28 '22 02:09

Ingo Karkat


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.

like image 22
mb14 Avatar answered Sep 28 '22 01:09

mb14