tags:

views:

109

answers:

7

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

before:

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

after:

sentence1.

sentence2.

sentence3

sentence4.

sentence5.

A: 

Just replace all end of sentences /(?<=.) / with a period followed by two newline characters /.\n\n/. The syntax would of course depend on the language you are using.

Michael Goldshteyn
+1  A: 

Using Perl:

perl -e "$_ = <>; s/\.\s*/.\n/g; print"

Longer, somewhat more readable version:

my $input = 'foo. bar. baz.';
$input =~ s/
    \.      # A literal '.'
    \s*     # Followed by 0 or more space characters
    /.\n/gx;       # g for all occurences, x to allow comments and whitespace in regex
print $input;

Using Python:

import re
input = 'foo. bar. baz.'
print re.sub(r'\.\s*', '.\n', input)
Cameron
+1  A: 

You can use a regex

/\.( |$)/g

That will match the end of the sentence, then you can add newlines.

Or you can use some split function with . (dot space) and . (dot), then join with newlines.

BrunoLM
`.` generally matches any single character in a regex, and should be escaped.
Cameron
@Cameron: I had already noticed and I've just fixed. :P
BrunoLM
@Bruno: Sorry, I commented too soon ;-)
Cameron
A: 

An example using Ruby:

ruby-1.9.2 > a = "sentence1. sentence2.  sentence3. and array.split(). the end."
 => "sentence1. sentence2. sentence3. and array.split(). the end." 

ruby-1.9.2 > puts a.gsub(/\.(\s+|$)/, ".\n\n")                                
sentence1.

sentence2.

sentence3.

and array.split().

the end.

It goes like, for every . followed by (1 whitespace character or more, or followed by end of line), replace it with just . and two newline characters.

動靜能量
A: 
Patrick Krecker
A: 

using awk

$ awk '{$1=$1}1' OFS="\n" file
sentence1.
sentence2.
sentence3.
sentence4.
sentence5.
sentence6.
sentence7
ghostdog74
+3  A: 

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
Al
@Al: You may want to use this regex from `:help /character-classes` as it closely mimics "sentences" defined by the `)` command: `[.!?][])"']*\($\|[ ]\)`
Randy Morris