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

别来无恙 提交于 2019-12-25 00:38:13

问题


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.


回答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



回答2:


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



来源:https://stackoverflow.com/questions/23625809/for-selected-lines-put-each-scentence-on-a-new-line-with-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!