Skip to content

Instantly share code, notes, and snippets.

@floscr
Created May 29, 2019 16:05
Show Gist options
  • Select an option

  • Save floscr/74b095e7d0eb99cced2ce8dc9280ca6d to your computer and use it in GitHub Desktop.

Select an option

Save floscr/74b095e7d0eb99cced2ce8dc9280ca6d to your computer and use it in GitHub Desktop.
Sorting Lines by Regexp
# Created 2019-05-29 Wed 18:04
#+TITLE: Sorting Lines by regexp
#+AUTHOR: Florian Schrödl
Say you have the code, andd you want to sort by the ~key~
#+begin_src elisp
(map! :leader (:desc "Notes" :prefix "n"
:desc "Pinboard File" :n "B" (λ! (find-file org-pinboard-file))
:desc "Reading List" :n "r" #'+org|org-open-reading-list-file
:desc "Home" :n "h" #'+org|org-open-home-file
:desc "Search Pinboard" :n "b" #'helm-org-pinboard
:desc "Inbox" :n "i" (λ! (find-file (concat org-directory "/inbox.org")))
:desc "Emacs" :n "e" (λ! (find-file (concat org-directory "/inbox.org")))
:desc "Work" :n "w" #'+org|org-open-work-file
:desc "Agenda" :n "a" #'org-agenda
:desc "Store Link" :n "y" #'org-store-link))
#+end_src
You can then sort by matching groups with ~sort-regexp-fields~
#+begin_example
Regexp specifying records to sort : ^.*:n "\(.\)".*$
Regexp specifying key within record : \1
#+end_example
Which results in
#+begin_src elisp
(map! :leader (:desc "Notes" :prefix "n"
:desc "Pinboard File" :n "B" (λ! (find-file org-pinboard-file))
:desc "Save All Org Buffers" :n "S" #'org-save-all-org-buffers
:desc "Agenda" :n "a" #'org-agenda
:desc "Search Pinboard" :n "b" #'helm-org-pinboard
:desc "Emacs" :n "e" (λ! (find-file (concat org-directory "/Emacs.org")))
:desc "Home" :n "h" #'+org|org-open-home-file
:desc "Inbox" :n "i" (λ! (find-file (concat org-directory "/inbox.org")))
:desc "Reading List" :n "r" #'+org|org-open-reading-list-file
:desc "Work" :n "w" #'+org|org-open-work-file
:desc "Store Link" :n "y" #'org-store-link))
#+end_src
You could even *just* sort the columns, leaving the rest of the code intact, with
#+begin_example
Regexp specifying records to sort : :n "\(.\)"
Regexp specifying key within record : \1
#+end_example
#+begin_src elisp
(map! :leader (:desc "Notes" :prefix "n"
:desc "Pinboard File" :n "B" (λ! (find-file org-pinboard-file))
:desc "Save All Org Buffers" :n "S" #'org-save-all-org-buffers
:desc "Reading List" :n "a" #'+org|org-open-reading-list-file
:desc "Home" :n "b" #'+org|org-open-home-file
:desc "Search Pinboard" :n "e" #'helm-org-pinboard
:desc "Inbox" :n "h" (λ! (find-file (concat org-directory "/inbox.org")))
:desc "Emacs" :n "i" (λ! (find-file (concat org-directory "/inbox.org")))
:desc "Work" :n "r" #'+org|org-open-work-file
:desc "Agenda" :n "w" #'org-agenda
:desc "Store Link" :n "y" #'org-store-link))
#+end_src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment