1

I'm using the most recent version of nvim on arch.

I have sentences in my text like this

The
brown fox

I wish to use this format
:%s///g
to replace all lines starting
\nlowercase letter + rest of line with
lowercase letter + rest of line

ie shift the line starting lowercase up to be joined to the line above it, like using the "J" key.

Result should be like this

The brown fox

Want to do this for all sentences globally.

How can I do this in Vim?

Kes
  • 663
  • 1
  • 8
  • 18

3 Answers3

4

I'm pretty sure a very similar question was already asked here. Anyway, here's a simple trick:

:g/^\l/-join

That is, for every line starting with lowercase letter join it with the previous one.

Matt
  • 20,685
  • 1
  • 11
  • 24
2

This solution worked

:%s/\n\([a-z]\)/ \1/g

I am familiar with the %s///g format but had previously struggled, for whatever reason with putting regex into vim's search and replace command.
Don't know why becasue as I now see it's as easy as any other regex substitution.

Kes
  • 663
  • 1
  • 8
  • 18
  • 1
    Nice one! A few simplifications are possible: :%s/\n\ze\l/ /. I usually like to use \zs and \ze to mark start and end of match, I find them to be a lot more convenient than using groups and backreferences... – filbranden Dec 19 '20 at 16:15
0

It just happened to me. Thanks @matt for the solution. I changed for my usage

:g/^[^0-9]/-join

^[^0-9] - any line that starting with non-number character

  • 1
    @VivianDeSmedt arguably it is an answer, if a slight modification to another, and it refers to that one. It’s not a strong answer, but some edits could change that. Deserves to stand, in my opinion. – D. Ben Knoble Feb 11 '23 at 15:51
  • Hi @Ben you are right but at the time I left the comment the answer was only a pure Thanks. I'll delete my comment since it is now irrelevant ;-) – Vivian De Smedt Feb 11 '23 at 16:09
  • 1
    @VivianDeSmedt oh, didn't see the edit history. Thanks for suggesting the improvements! – D. Ben Knoble Feb 12 '23 at 15:31