Exporting literal code blocks verbatim from org-mode
Nov 12, 2023 (updated Jul 1, 2024). 263 words. 2 minutes. In Tech. Emacs orgmode
tl;dr:How to export verbatim org-mode code with one single character.
The problem #
I just wanted to share one quick lesson that I learned by accident today for future me and everyone, who stumbles in here via Google et al.
While I was writing a blog post, I came upon the following challenge: I want to
export an org-mode code block itself, i.e., #+BEGIN_SRC ... #+END_SRC
verbatim, because I was writing about literal configs in org-mode. I could
probably export the whole snippet as verbatim somehow, but I want it displayed
as a code block with line numbers and stuff, so it must be wrapped inside
another code block. Simply nesting the two does not work, as the first
#+END_SRC terminates the whole block.
The solution #
Google did not turn up any results or (more likely) I was trying the wrong search query, but by accident I found a solution myself.
When I pasted a few lines of org-mode config (#+PROPERTY fields to be
specific, but I do not think that it matters) into an org-mode code block,
Emacs/org-mode automatically added a , in front of every line. Testing this
solution for other org-mode code, like #+BEGIN_SRC lines, proved that this is
indeed the solution:
| |
By the way, to display this code snippet I had to nest the whole thing twice, so three code blocks in total, one of which you cannot see.
Neat solution for a rather niche problem, very happy to have solved this one! Hope it helps anyone else staring at the same issue.
Other posts related to this one: