5

I'm wondering if there's a suggested way to format a terminal command and its output for posting on the site. For example, the Markdown code

```
user@host:~$ date
Sat Apr 13 12:50:49 PDT 2019
```

produces

user@host:~$ date
Sat Apr 13 12:50:49 PDT 2019

This code block doesn't have any colorization or syntax highlighting, which are things often used in terminals to distinguish the prompt, command and output. So I'm wondering if there's a better or more preferred way to format something like this. I've also tried using the following language identifiers

  • ```shell

  • ```bash

  • ```console

but they don't seem to change the result.

On a similar note, I'm wondering if there's a preferred way to denote the prompt. In my example, I used user@host:~$ date, but I could also use $ date or something similar.

2
  • 2
    The output doesn't have any syntax highlighting? Why would it have? As for the prompt, I tend to trim it down to only show either $ or #, whichever is appropriate (less distractive that way, and just shows that it's something being typed at an interactive shell prompt).
    – Kusalananda Mod
    Apr 13, 2019 at 21:38
  • 1
    I guess I meant that the entire block of code doesn't have any syntax highlighting or colorization to distinguish the prompt, command, and output. I've edited my question.
    – Trevor
    Apr 13, 2019 at 21:54

1 Answer 1

8

You can get syntax coloring for scripts by starting the code block with something like ```lang-sh. There's no syntax coloring that would detect prompts in a terminal session, and it would be difficult to make one due to the diversity of prompts.

There's no way to use color except via syntax coloring for the supported languages. You can get bold and italics in code blocks if you use HTML markup:

Ordinary text, emphasized, bold, struck out
Note that &, < and > need to be entered as HTML entities.

This was typeset with

<pre>
Ordinary text, <em>emphasized,</em> <strong>bold,</strong> <strike>struck out</strike>
Note that &amp;, &lt; and &gt; need to be entered as HTML entities.
</pre>

Unless the prompt is part of the question, I recommend to avoid fancy prompts. If there's no other context, stick to a minimalistic prompt to minimize confusion: $ for ordinary users in an sh shell, # for root, % for zsh, > for csh. In the context of a specific distribution, it might be preferable to use the default prompt on that distribution, but that can be confusing if it includes a hostname or username that isn't recognizable as such.

2
  • 2
    So is ```lang-sh different than ```sh?
    – Trevor
    Apr 13, 2019 at 23:03
  • 6
    @wxyz On this specific site for this specific tag, no, but in general, yes. sh means to use the syntax coloring that the moderators have declared for the sh tag. lang-sh means to use the syntax coloring for the sh language. Apr 13, 2019 at 23:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .