3

I'm looking for a solution about a "paste" problem on vi/vim .

How to join specific lines in a text file? I have a file like this:

15/10/2011 Revolution day
16/10/2011 About Rome
 - differents newspapers
  blogs an feeds
 :) hp
17/10/2011 Yep, another day

and I want to paste every line that does not start with a number. I've tried something like this, but it does not work:

:[!^[0-9]]!paste -d ' ' -s
3
:%s/\(.*\)\n\(^[^0-9]\)/\1\2/g

If you want a space or something between, add it between \1 and \2.

2

you can do it in two steps in VIM, if that's acceptable:

First, join all lines into a single line.

with the file open in VIM, issue the following keystroke: VGJ

  • V = Visual Mode (linewise, rather than character-wise (v))
  • G = highlight all lines
  • J = Join all lines

Second, split your lines on the date format the resulting lines start with.

with the file open in VIM, issue the following command:

:%s#\([0-9][0-9]/[0-9][0-9]/[0-9]\)#^M\1#g

This will search the single joined line for any string that matches NN/NN/N, where N = 0-9, which matches the date format that your lines start with, and will split on that match.

2

Use the v with join.

:v/^[0-9]/-1,join

Broken down:

  • :v select any lines that do not match regular expression
  • /^[0-9]/ any line starting with a digit
  • -1, the previous line to the current line
  • join combine the lines selected, separated by whitespace
0

I'm talking about vim, this will not run in vi. Be aware of that.

What about

    :%v/^[^0-9]/d

to delete all lines in buffer , which start with a number.

    ggVG

to highlight all lines

    :w ! paste -d ' ' -s

and after that

    u

to go back to the starting point

  • It does not seems to work. Morevorer, I have stated an incorrect question. I want to join every line not starting by a number to the previous line starting with a number. – gogolb Oct 28 '11 at 13:34
0

please try:

:g/^\d/,/^\d/-1j

and ignore error message E16: Invalid range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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