Sorting Org Tags using Org Mode!

[James Dyer] : Apr 07, 2023 : 246 words
emacs linux 🏷️ org emacs elisp 2023

Well as always a little more time with emacs a little feedback and then finding more about org I have now figured out (I think) how I can better sort tags in an org file. In my previous post:

Sorting Org Tags

I made the following comment:

I had assumed that org-mode came with the built-in ability to sort tags but I couldn’t find any evidence of this

Well as it turns out there is evidence of this! and it takes the form of:

(setq org-tags-sort-function 'org-string-collate-greaterp)

This is tied into C-c C-q (org-set-tags-command) of which I hadn’t yet discovered but now I think I shall bring into my muscle memory. I had previously just been manually adding tags!

Further interwebs hunting and I came across the following function:

(defun my/org-sort-tags ()
  "On a heading sort the tags."
  (interactive)
  (when (org-at-heading-p)
    (org-set-tags (sort (org-get-tags) #'string<))))

which more concisely accomplishes my original intention and will work when the cursor is anywhere on an org heading and therefore a macro or presumably an org-* type iterator would neatly take care of the whole file for me.

Although I went down a little rabbit hole I found a few things down there, elisp improvement, digging more around org-mode and bewilderingly wondering where my journey will end. Of course with emacs it never will, my little head will just peep out at the daylight sun every now and then but my little noggin will be wiser and more proficient 😃