When we add tags to a headline, they just get appended. For instance,

* Head 1 :A:C:B:
* Head 2 :B:A:C:

Is there a way I can sort these tags so we get:

* Head 1 :A:B:C:
* Head 2 :A:B:C:

You can sort tags alphabetically using:

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

Documentation: When set, tags are sorted using this function as a comparator.

  • Thanks. But how is it supposed to work? I added that line to ~/.doom.d/config.el (I use Doom Emacs). But nothing happens. Tags get added just as usual --- unsorted, in the order in which they are added
    – deshmukh
    Mar 20 '20 at 9:41
  • 1
    The sort applies when you set the tags using the org-set-tags-command (C-c C-q).
    – Marco Wahl
    Mar 20 '20 at 12:10

I don't think it's already in Org.

What about

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

for a start?

Activate with

M-x my-org-sort-tags
  • Thanks. This works. But is there a way to make it work automatically? So, the tags are sorted as they are added.
    – deshmukh
    Mar 20 '20 at 9:43
  • Okay, it is already in Org see the other answer from [user:jagrg].
    – Marco Wahl
    Mar 20 '20 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.