0

My vim buffer contains lines with more then one sentence.

How can I make each sentence start on an new line? I do not want to insert extra empty lines between sentences that already start on a new line.

I can replace each . by .\n %s/\./\.\n/ but that insert a new line also when there already is a new line after a sentence.

Edit: If the line starts with % then I want to leave that line as it is.

sjdh
  • 3,907
  • 8
  • 25
  • 32
  • seems to be a duplicate http://stackoverflow.com/questions/3956375/regex-attach-a-newline-to-every-sentence-using-vim – Xavier May 13 '14 at 08:06
  • @angezanetti Thanks, the questions are very similar indeed. Only I do not want to insert a new line when the next sentence already starts at a new line. This part is omitted in http://stackoverflow.com/questions/3956375/regex-attach-a-newline-to-every-sentence-using-vim – sjdh May 13 '14 at 08:14
  • it is very hard to be done precisely in vim. It depends on how "complex" your sentences are. – Kent May 13 '14 at 09:02
  • See discussion here: http://vi.stackexchange.com/questions/2846/how-to-set-up-vim-to-work-with-one-sentence-per-line – Nathan Long May 21 '16 at 10:55

2 Answers2

1

You can try this:

:%s/\([?.!]\)\s\(\w\)/\1\r\2/g

A lookbehind to make sure the line does not start with a % should prevent substitution on those lines:

:%s/\(^%.*\)\@<!\([?.!]\)\s\(\w\)/\2\r\3/g
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Thank you. Can you add a bit of explanation? – sjdh May 13 '14 at 08:15
  • It replaces the space characters between all sentence ending characters (?.!) and word characters. The captures remember the sentence enders and the first letter of the words. The substitution replaces the space with a newline. – perreal May 13 '14 at 08:21
  • Thanks a lot for your explanation. This forms a good start to learn to build these kind of replacements. Thank you for the update too. Can you also explain the update? – sjdh May 13 '14 at 09:23
0

Try this:

:g/^[^%]/s/\../.^M/g

Explanation:

:g/^[^%]/ work on lines that don't start with %

s/\../.^M/g replace every . followed by another character with a newline.

"one. two. three." becomes
one.
two.
three.

This doesn't keep the character after the full stop.

To keep it, use this:

:g/^[^%]/s/\../&^M/g

"one. two. three." becomes
one. [trailing space]
two. [trailing space] 
three.

To keep it, but on the line following, use this:

:g/^[^%]/s/\.\(.\)/.^M\1/g

"one. two. three." becomes
one.
 two.
 three.

In all cases, to enter ^M, type ctrl+V then return

Chris Lear
  • 6,592
  • 1
  • 18
  • 26