How to show terminal/shell code snippets in Sphinx?

24,564

Solution 1

You can indent the text you want to be written as code, and precede that with a double colon. For example:

Some body text explaining what's coming up::

    ls -lsa .
    make file

Or it also turns out you can get away without any text before, just using the double colons. In the first case, one colon gets rendered, while for this case, just the code gets rendered.

::
    mkdir test
    cd !$  

Then the commands will come out in a fixed-width font, and, depending on the style sheets you've chosen, highlighted as well (by default, highlighted with green background).

You can also inline-highlight with backticks, e.g. `ls -lsa .`.

Solution 2

.. code-block:: console

is meant for interactive sessions. Bash or sh didn't work for me.

( from http://build-me-the-docs-please.readthedocs.org/en/latest/Using_Sphinx/ShowingCodeExamplesInSphinx.html#pygments-lexers )

Solution 3

For Linux console commands you could use bash or sh:

.. code-block:: bash

   $ ls -lsa .
   $ make file

Highlighting wouldn't probably work right all the time, but at least you give it a chance.

You can also set the default highlighting in the beginning of the file if you use the @Bonlenfum way with double colons:

.. highlight:: sh
Share:
24,564
Shengjie
Author by

Shengjie

Enthusiastic software engineer, a big fan of open source community development, big data, cloud technologies etc...

Updated on December 20, 2020

Comments

  • Shengjie
    Shengjie over 3 years

    Using Sphinx, I know you can show a code snippet by having something like:

    .. code-block:: ruby.
      ${your ruby code goes here} 
    

    How can I show a code snippet of terminal or shell commands not related to any languages? I don't care if they are not highlighted properly syntax wise, I just want them show up in the doc as a piece of code other than normal text.

    Example

    $ ls -lsa . 
    $ make file