In org mode and in a source code special block, I can add code line labels, which have ref: in the label -- but when I make a link there, I have to omit the ref:. For example:

#+begin_src csharp
1044 string foo = someFunction(1234);
1045 if (foo == "1")
1046 {
1047     bar = 5; //  (ref:my-code-line-label)
1048 }
// etc etc

There's a bug [[(my-code-line-label)][right here]].

This seems inconsistent. Why do I have to omit the ref: bit? This always confuses me; it seems like it would be simpler to just make the label and the links to that line of code be exactly the same. I don't see any particular reference to this in the org manual. Any insights?

  • No particular insights as to why, but the how is clear: org-link-search takes the undecorated label from the link (i.e. without the ref:) as argument and decorates it internally. By default, it adds the ref: part, and creates a regexp for it (including the parens as part of the match) and that's what it searches for. If you pass ref:foo, then it looks for (ref:ref:foo) and that fails. There is some provision to change that default using a :label-fmt property, but I don't know how to trigger that. You might find something through git archaelogy or by posting a question on the ML
    – NickD
    Commented Jun 27, 2023 at 16:28
  • Thanks for the investigation! Here's my idea (which I should send to the org-mode list): get rid of the special behavior for code line labels and just make this work with dedicated targets: in regular parts of my org file, I use <<link-target>> and can make links to that. It would be simple and consistent to make that also work the same when <<link-target>> is inside a code block.
    – Dan Drake
    Commented Jul 3, 2023 at 12:17

1 Answer 1


See this thread on the org-mode mailing list for some background. There, Ihor Radchenko pointed me to org-coderef-label-format:

the way coderefs appear in the code are completely configurable using org-coderef-label-format ("(ref:%s)" by default) and using -l src code switch.

And if I customize that to just (%s), I get the kind of consistent behavior that I'm looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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