Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach a newline to every sentences

Tags:

regex

vim

i was wondering how to turn a paragraph, into bullet sentences.

before:

sentence1. sentence2.  sentence3.  sentence4.  sentence5.  sentence6.  sentence7. 

after:

sentence1.

sentence2.

sentence3


sentence4.

sentence5.
like image 434
pao Avatar asked Oct 18 '10 03:10

pao


1 Answers

Since all the other answers so far show how to do it various programming languages and you have tagged the question with Vim, here's how to do it in Vim:

:%s/\.\(\s\+\|$\)/.\r\r/g

I've used two carriage returns to match the output format you showed in the question. There are a number of alternative regular expression forms you could use:

" Using a look-behind
:%s/\.\@<=\( \|$\)/\r\r/g
" Using 'very magic' to reduce the number of backslashes
:%s/\v\.( |$)/.\r\r/g
" Slightly different formation: this will also break if there
" are no spaces after the full-stop (period).
:%s/\.\s*$\?/.\r\r/g

and probably many others.

A non-regexp way of doing it would be:

:let s = getline('.')
:let lineparts = split(s, '\.\@<=\s*')
:call append('.', lineparts)
:delete

See:

:help pattern.txt
:help change.txt
:help \@<=
:help :substitute
:help getline()
:help append()
:help split()
:help :d
like image 159
DrAl Avatar answered Oct 20 '22 19:10

DrAl