Search code examples
sortingvimemacsvinano

Sort for specific lines in text


I have the given text:

# Blub
Hello this is a blub text.

# Bla
This is the bla text.

# Abba
Another text.

Is it possible to sort for the lines with the #? So that the resulting text is:

# Abba
Another text.

# Bla
This the bla text.

# Blub
Hello this is a blub text.

Preferably using vim or emacs.


Solution

  • In Emacs,

    1. M-xsort-regexp-fields
    2. Enter: #[^#]*
    3. Enter: \&

    The first regexp delimits the record, and the second specifies the key for sorting.

    If you're at liberty to choose the marker character and use * instead of #, you may use org-mode's command org-sort-entries instead, which saves you from entering the regexps.