I'd like to write examples of shell commands in an Org document:

Here's an inlined ~if~ statement:
  $ if true; then echo hello; fi

I wish for the command part, starting with $, to be fontified as in shell-script-mode. Is there a way to do this? For instance, by creating a major mode deriving from shell-script-mode that only looks at lines starting with $? Or by tricking font-lock into thinking that $ is a single character comment and lines starting with anything but $ are comments?

  • 1
    I got you, there should be a major-mode for it, named like "shell-history-mode" maybe? I looked for this several times. If you care only about exporting, there is "shell session" support in prismjs.com
    – nichijou
    Jan 2 at 10:30
  • @nichijou: Neat! I added an advice to org-html-fontify-code to add <code class="language-shell-session">, and it looks nifty. Thanks! I'll keep the question open since I'd prefer a solution within Org, but in the meantime, this definitely helps!
    – Michaël
    Jan 2 at 13:26

1 Answer 1


EDIT: update w.r.t. comments

So the final solution/workaround is:

  • use source blocks, as they include highlighting when exported
  • comment out, i.e. put # before, the self written output lines
  • then in the function org-html-src-block replace the line
(code (org-html-format-code src-block info))


       (code (with-temp-buffer (insert (org-html-format-code src-block info))
                                   (goto-char (point-min))
                                   (while (not (eobp))
                                     (print (search-forward ">"))
                                     (if (looking-at "# ")
                                         (replace-match "")
                                       (insert "$ "))
                                   (print (buffer-string))))

(where the search-forward code probably has to get slightly improved for inputs without fontification)


I think you should use a source block for this instead, e.g. as follows

#+begin_src sh :exports both
  if true; then echo hello; fi

You will get the output by running/exporting it (the :exports both header argument makes that both the code and the result get exported).

Then subsequently you can format exported output how you prefer it. You can define a derived backend for this (so I guess you should create e.g. my-sh source blocks).

You can read more about it here and here. Especially check out the function org-export-define-derived-backend in the latter link. You can then set your personal my-org-html-src-block translator function (that you can adapt from the original org-html-src-block function) for source blocks in the :translate-alist. (To understand the code in the org-html-src-block function, I would recommend to use edebug. I guess you can get rid of most of the code, and just keep and modify 'the essence' of it).

  • Thanks for the reply! I believe this is "avoiding" my main problem, by changing my input. I'd very much rather keep my blocks as they are right now, with inputs and outputs written by me. Most of them contain multiple commands, a good portion use the standard input, and all of this would be a pain to do with Org (if doable at all).
    – Michaël
    Jan 4 at 14:10
  • That's fine of course. However, I don't think multiple commands per se are a problem in org. And also I am wondering what you mean with the commands using stdin. How you use stdin in example blocks? Jan 4 at 17:25
  • Oh, well, it's as in a shell prompt. Something like: $ sed s/i/a/ input anput [Ctrl-d] If you want to have a quick look at my document, it's there: mc.cdm.depaul.edu/sysII/shelllab.html
    – Michaël
    Jan 4 at 23:07
  • That is a great document! So did you already find out the answer to this question (your document looks like you might have)? Otherwise, how do you get the highlighted example blocks? You did not declare a language in the header of your example block in your example (I wasn't aware that this works with example blocks, otherwise I might have given another answer, so I think you should add it to your example here). But even with the language in the header, I don't know how you get it exported including the highlighting (maybe you could add your solution to your question). Jan 5 at 9:52
  • I don't know your exact motivation for this question. But to get the behavior in your exports, as a workaround, you could replace the $ in your examples e.g. with ; (which does not mess up the fontification) and then add a regexp-replace at the end of org-html-example-block to replace the ; at the beginning of lines with $ . Jan 5 at 9:54

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.