How can I break a paragraph into sentences with Vim?

4

1

By breaking into sentences I mean that each new sentence should start with a new line.

How to repeat )i<CR><Esc> to the end of the paragraph }? (<CR> = Enter)

If I make a macro )i<CR><Esc> as "q", can I execute it until the end of the paragraph?

kirill_igum

Posted 2011-04-26T00:04:02.003

Reputation: 1 098

Answers

2

You can do a search and replace. I just wrote this out. It works, but you could probably do better.

:%s/\v[ ]*([^\.]*\.)/\1\r/g

Dan Loewenherz

Posted 2011-04-26T00:04:02.003

Reputation: 261

2

vap:s/\. /.^M/g
  • vap selects your current paragraph
  • :s/\. /.^M/g replace all periods followed by a space with a period followed by a newline. (Note that to get that literal newline (^M) in the replacement expression, you'll have to type <CTRL-V><CR>.)

chisophugis

Posted 2011-04-26T00:04:02.003

Reputation: 193

0

I found that there is a very easy way of doing this using vim-macros.

  • First in normal mode press q, then any key, suppose that key is a. So you will see something at the bottom as recording @a.
  • Now follow the key-sequence as 0)i<cr><esc>q Now your macro is recorded. This will break the paragraph at . .
  • Now, repeat this macro as many times by pressing N@a in normal mode, where N is any number.

Note: You can choose any alphanumeric character to store your macro than a, this macro breaks the line at ., you can choose any column just replace . by any N of your choice.

Galilean

Posted 2011-04-26T00:04:02.003

Reputation: 101

0

My solution, start in normal mode and type:

vip:'<,'>s/\n/ /|'<,'>s/\([.?!]\)\s/\1\r/g

Note that the first '<,'> will automatically be inserted on the command line when you press the ":" key after typing "vip" in normal mode.

The first substitute joins the paragraph into one line, then the "|" character delimits a second :-command within the same command line, and this time the "'<,'>" must be typed by hand. The second substitute command replaces a period, question mark, or exclamation mark followed by a space with the matching symbol and a newline.

This won't catch instances where a sentence ends with a period/excalmation/question and a quote character, etc. but the pattern can be extended to do so.

Heptite

Posted 2011-04-26T00:04:02.003

Reputation: 16 267