1

I have a dozen org-mode links in the standard format. How can I sort these by the descriptive text?

[[alink1][Baz]]

[[alink2][Foo]]

[[alink3][Bar]]

These are displayed like this in the buffer:

Baz

Foo

Bar

The result should be:

[[alink3][Bar]]

[[alink1][Baz]]

[[alink2][Foo]]

Which will be show as:

Bar

Baz

Foo

[Update]

When I do this: (setq org-descriptive-links t) then try M-x org-sort-list, I get error message: 'Wrong type argument: number-or-marker-p, nil'

enter image description here

  • org-sort-list can take a function as parameter, which must return either a string or an integer used with the sort function. We could write a function that returns the descriptive text… – Ehvince Dec 18 '14 at 13:43
  • You screen shot shows that the first list marker - is actually part of the link text. This is what triggers the error. Move it outside the link, and then org-sort-list will work. – Andrew Swann Dec 22 '14 at 15:58
  • Actually, the problem is that I selected the region instead of simply placing the cursor on one of the lines. I debugged it from this other answer: stackoverflow.com/a/805491/2122052 – h4labs Dec 22 '14 at 16:23
  • @AndrewSwann And yes, for some reason org mode always has me in the middle of the description for the first line so any '-' or '+' is inserted into the description, which does cause a sorting problem. – h4labs Dec 22 '14 at 16:33
2

The easiest way I can think of is to store your links in an Org list

- [[alink1][Baz]]
- [[alink2][Foo]]
- [[alink3][Bar]]

and use M-x org-sort-list with the point somewhere in this list. As far as I can tell this works if org-descriptive-links is set to t.

Update:

As pointed out by @Ehvince, one can also use org-sort-list with a custom GETKEY-FUNC:

(defun my-get-link-text ()
  "Get the description of the first link on the current line."
  (save-excursion
    (if (re-search-forward org-bracket-link-analytic-regexp
                           (line-end-position) t)
        (match-string 5)
      (buffer-substring (point) (line-end-position)))))

(defun my-sort-list-of-links (with-case)
  (interactive "P")
  (org-sort-list with-case ?f #'my-get-link-text #'string<))

I also have some code for sorting links in a region (one per line) that does not use any Org functions (just org-bracket-link-analytic-regexp), but I don't think that's useful (given these two solutions).

  • Nop, it doesn't work – Ehvince Dec 18 '14 at 13:19
  • @Ehvince: It does work for me. Tested (again) using "emacs -Q"; Emacs version 24.4.1, Org version 8.2.10. – Constantine Dec 18 '14 at 15:18
  • Sorry: it doesn't work with org version 7.8.02 Emacs 24.3 and neither does your my-sort-list-of-links :( Will try updating Org. – Ehvince Dec 18 '14 at 16:18
  • @Ehvince: What about the second approach (using a custom getkey-func)? Still no luck? (I don't think I want to install Org 7.8 to try it myself.) – Constantine Dec 18 '14 at 16:21
  • still no luck, which is surprising (see second half of my previous comment ;) ) – Ehvince Dec 18 '14 at 16:27

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.