There appears to be a bug in `org-fill-template' (in lisp/org-macs.el): keys to be expanded are sorted by increasing length, so that "noweb" is processed before "noweb-ref". As a result, if a template includes "%noweb-ref", `org-fill-template' will expand it as "%{noweb}-ref" rather than "%{noweb-ref}". As far as I can tell, this bug has existed since `org-fill-template' was added, in e8ef16306ca56af2dceb9e2be0b57c930e9b5584 . I tried to trace it as back as I could, to see whether there was a good reason for sorting keys this way, but couldn't find one. I'm including a patch with a test to find buggy behavior, and another to fix it.