Highlight bash/shell code in markdown

2019-03-07 20:36发布

How to highlight the bash/shell commands in markdown files?


For example to highlight js I write:

```js
function () { return "This code is highlighted as Javascript!"}
```

To highlight HTML code I use ```html.

How can we highlight bash/shell commands?

6条回答
虎瘦雄心在
2楼-- · 2019-03-07 20:54

If you are looking to highlight a shell session command sequence as it looks to the user (with prompts, not just as contents of a hypothetical script file), then the right identifier to use at the moment is console:

```console
foo@bar:~$ whoami
foo
```

GitHub Markdown preview tab screenshot

查看更多
Luminary・发光体
3楼-- · 2019-03-07 20:58

Per the documentation from GitHub regarding GFM syntax highlighted code blocks

We use Linguist to perform language detection and syntax highlighting. You can find out which keywords are valid in the languages YAML file.

Rendered on GitHub, console makes the lines after the console blue. bash, sh, or shell don't seem to "highlight" much ...and you can use posh for PowerShell or CMD.

查看更多
老娘就宠你
4楼-- · 2019-03-07 21:03

using the knitr package:

```{r, engine='bash', code_block_name} ...

e.g.:

```{r, engine='bash', count_lines}
wc -l en_US.twitter.txt 
```

you can also use:

  • engine='sh' for shell
  • engine='python' for python
  • engine='perl', engine='haskell' and a bunch of other C-like languages and even gawk, awk etc.
查看更多
孤傲高冷的网名
5楼-- · 2019-03-07 21:09

I find good description at https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code

Code blocks are part of the Markdown spec, but syntax highlighting isn't.

However, many renderers -- like Github's and Markdown Here -- support syntax highlighting. Which languages are supported and how those language names should be written will vary from renderer to renderer. Markdown Here supports highlighting for dozens of languages (and not-really-languages, like diffs and HTTP headers); to see the complete list, and how to write the language names, see the highlight.js demo page.

Although I could not find any official git hub doc about using highlight.js, I've tested lots of languages and seemed to be working

To see list of languages I used https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html#language-names-and-aliases

Some shell samples:

Shell:      console, shell
Bash:       bash, sh, zsh
Powershell: powershell, ps
Dos:        dos, bat, cmd

Example:

```bat
cd \
copy a b
ping 192.168.0.1
```
查看更多
乱世女痞
6楼-- · 2019-03-07 21:13

Depends on the markdown rendering engine and the markdown flavour. There is no standard for this. If you mean github flavoured markdown for example, shell should work fine. Aliases are sh, bash or zsh. You can find the list of available syntax lexers here

查看更多
劫难
7楼-- · 2019-03-07 21:14

Bitbucket uses CodeMirror for syntax highlighting. For bash or shell you can use sh, bash, or zsh. More information can be found at Configuring syntax highlighting for file extensions and Code mirror language modes.

查看更多
登录 后发表回答