Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort paragraphs according to their headings in Vim?

Tags:

vim

sorting

Let's say we have the following paragraphs that are separated by a blank line from each other:

B Heading
Lorem ipsum 1.
Lorem ipsum 2.

A Heading
Lorem ipsum 3.
Lorem ipsum 4.

How to sort these paragraphs with respect to their headings and obtain the following text?

A Heading
Lorem ipsum 3.
Lorem ipsum 4.

B Heading
Lorem ipsum 1.
Lorem ipsum 2.
like image 728
Mert Nuhoglu Avatar asked Jun 07 '14 15:06

Mert Nuhoglu


1 Answers

One solution is to concatenate your paragraphs before sorting.

Say that you do not use the @ symbol in your text, you can use:

:%s/\(.\+\)\n/\1@/

to do that. Then you can sort your lines with

:sort

and at last proceed to the reverse operation to get your paragraphs back:

:%s/@/\r/g
like image 50
Qeole Avatar answered Nov 07 '22 23:11

Qeole