Is there a way to enable line numbers only for the source code blocks between #+BEGIN_SRC and #+END_SRC?

  • 2
    There is no built-in support for such a feature. The line numbers that are built-in get generated during redisplay by the C internals. Prior to that implementation, there were a couple of libraries that generated line numbers with lisp ... but the feature was slower in long buffers due to line-number-at-pos. It is possible to modify the linum.el library or nlinum.el to create the feature of line numbers for source code blocks, but that would be a nontrivial undertaking. Attempts to limit line numbers in Lisp to only the visible windows is also nontrivial, and not 100% accurate.
    – lawlist
    Nov 10 '21 at 16:57
  • 2
    To the extent the O.P. is interested in displaying line numbers on the output / export of evaluated src code blocks, see: stackoverflow.com/questions/12170382/…
    – lawlist
    Nov 10 '21 at 18:17

I'm not sure how to have it only display in Babel blocks, but I can suggest how to get line numbers displaying only when you're inside a Babel block.

You can turn on line-num mode if you're in a Babel block with:

(if (org-in-src-block-p) ...

And continuously check if you're in a Babel block by running (add-hook 'post-command-hook 'my/line-num-babel).

Finally, you can take some hints here to change where line number 1 is located: Zero-indexed line numbering. In fact, the second answer there with the comment Update line numbers for the portion visible in window WIN. may be able useful for only showing line numbers in Babel blocks.

In summary, this is Emacs, so you can absolutely do what you want. But it might not exist. If you write it and release it as a package, I assume others would be interested in using this feature.

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.