Sort/Reverse Sections in Markdown (with VIM)

Viewed 192

I have a Markdown file for logging. Each section has a heading named by date in chronological order. Part of the file is the following.

## 20200628
- Traceback
- [x] code debug
- [x] read Section 2

## 20200629
- Homepage rebuilding

## 20200630
- 13:00: Meeting Tom

I would like to sort the logs in a reverse way, after which it will show like the following.

## 20200630
- 13:00: Meeting Tom

## 20200629
- Homepage rebuilding

## 20200628
- Traceback
- [x] code debug
- [x] read Section 2

I was expecting to use VIM but I didn't figure out what is a proper way to achieve this. Any suggestions would be appreciated a lot. Thanks in advance.

1 Answers

We capture each block using a regex that matches its start, then we delete it and paste at the beginning of the file.

:$put _ | g/^## 202006[2-3][890]/normal! dapggP

The :$put _ part was used to add a blank line at the end, so each block has a blank line after it.

Related