sjdh
sjdh

Reputation: 4006

For selected lines, put each scentence on a new line with vim

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.

Upvotes: 0

Views: 393

Answers (2)

Chris Lear
Chris Lear

Reputation: 6742

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

Upvotes: 0

perreal
perreal

Reputation: 97918

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

Upvotes: 1

Related Questions