Johnny
Johnny

Reputation: 45

VIM One sentence per line: Separated by periods

I have a plain text that I would like to place each sentence on its own line. I see some answers for Perl but I am using Vim exclusively.

Upvotes: 0

Views: 1014

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172648

Those answers for Perl probably used regular expressions, and it should be relatively easy to convert the regular expression syntax to Vim's. :help perl-patterns should provide you with clues on how the syntax differs.

I'd like to propose a different solution that employs a recursive macro definition and Vim's sentences motion (:help (). First, a single replacement:

)hr<CR>

This jumps to the next sentence ()), then moves back to the separating whitespace (h), and replaces it with a newline (r<CR>).

To turn this into a macro that repeats itself (until some action cannot be fulfilled any longer and it aborts), first clear a macro register (e.g. a) and start recording: qaqqa. Then insert the above action, and conclude with a macro invocation @q. End the macro (q) and invoke it (@a). Complete macro (now using register q):

qqqqq)hr<CR>@qq@q

This will stop at empty lines and keep line breaks inside a sentence; you may want to :join all lines first. Alternatively, you can also tweak the replacement (r<CR>), maybe to a change command. Since you didn't mention any detailed requirements in your question, I'll leave it at that.

Upvotes: 2

xaa
xaa

Reputation: 846

I don't think the question is too unclear. I saw other questions much unclear with answers .. so .. let me try ?

If the sentences you want to separate are in one line and the "periods" only exist to separate the line then you can try the following command to split them:

:%s/\./.\r/g
     ^ ^ (change the dots by the character you want if needed)

This will look globally in the buffer for ., will subsitute it with itself (hmm) , and will add a line return (\r).

Then you may have to remove the indentation with :%le to format the lines to the left.

ps: that Rkta suggestion (from comment) %s/\([\.!\?]\)/\1\r/gc seems working fine as well (and is maybe better?). The Amadam comment is relevant as well.

Welcome to stackoverflow. read the stackoverflow.com/tour page may help your quest ;)

Upvotes: 2

Related Questions