How to highlight bash/shell commands in markdown? - bash

How can I 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?

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
```

It 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.

I found a good description at Markdown Cheatsheet:
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 GitHub documentation about using highlight.js, I've tested lots of languages and seemed to be working
To see list of languages I used https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md
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
```

If I need only to highlight the first word as a command, I often use properties:
```properties
npm run build
```
I obtain something like:
npm run build

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.

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.

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.

In Obsidian, you can use sh-session
https://prismjs.com/

Related

Detect prompt with xterm.js

TLDR: I want know how to detect from the output of a shell (e.g. zsh, bash) the location of the prompts (e.g. user#machine /etc % ).
Details
I have made a working shell frontend in the browser based on xtermjs. It is now equivalent feature-wise to e.g. the default macOS terminal application with zsh, bash and powershell. In a nutshell, it works by executing a shell process (e.g. zsh) as the child of a parent process that pipes the input/output from/to the browser via web sockets.
I want now to step up and implement a "collapse" functionality that hides the output of the selected commands in the history (like Visual Studio Code does now).
To this end, I need to detect the location of the prompts from the terminal output: the collapse function would then hide the characters between two consecutive prompts.
I know I can use the approaches below:
detect the prompt with a regular expression (I would need to parse the PS1 variable)
inject some special character sequence before and after the prompt (e.g. in variable PS1)
But both do not seem very robust, and may not work with some specific command interpreter. I could not find yet the location where this functionality is implemented in the source code of Visual Studio Code.
My question is: is there a robust way to achieve this functionality for at least zsh, bash and powershell (it is fine if it is specific to xterm.js)
Edit 1
This SO question is related: ANSI escape sequence for collapsing/folding text (maybe hierarchically)
It links to this interesting thread: https://github.com/PerBothner/DomTerm/issues/54
It appears that DomTerm uses escapes sequences at folding points (my solution 2).
Yet I don't see how to inject them into the terminal, besides hacking the PS1 env var.
Edit 2
While parsing iTerm's documentation I found out that it takes advantage of the hooks provided by the shell (e.g. for zsh) in order to print some special escape sequence at various locations, including before showing the prompt.
For example, in zsh, I can print string "🐮" before each prompt be executing precmd() { echo '🐮' }. Then when I execute e.g. ls I get
$> ls
[...]
🐮
$>
There is a more extensive explanation of the various available hooks for various shells here.
It looks like PowerShell uses a very different system though.

Jump to matching "done" while editing bash script in vi

In vi, the % key can be used to jump to a matching opening or closing parenthesis, square bracket or a curly brace.
Could it also be used to jump between matching "do" and "done" in a bash loop?
Not "vi" as such. vim does that.
There is a script matchit.vim which can do this:
The script is documented in the vim wiki page Moving to matching braces.
It part of the vim distribution, but is not installed (in your ~/.vim/plugin directory) by default. See the vim help for matchit-install for details.
Once installed, it has to be enabled, e.g., (see matchit.zip : extended % matching for HTML, LaTeX, and many other languages):
filetype plugin on
in your vimrc file.
For further reading:
Vim: Jumping to if endif in fortran uses simply runtime macros/matchit.vim rather than the two-part install from the vim wiki.
Matchit not working has several answers, some disagreeing.
The runtime macros/matchit.vim line in ~/.vimrc did not make matchit.vim work in my quick test; the procedure in matchit-install, plus the filetype line did work. As usual, your configuration may differ.
I maintain (and of course use) vi-like-emacs, and wrote comparable functionality for that editor using a different approach (see discussion of "fences" in documentation). In a quick test, I see that matchit.vim does not know about the syntax for case-values in a shell case statement. So there is some room for improvement.

Bash auto-completion highlighting

I'd like to know if it is possible to make bash auto-completion highlight the matched part of the auto-complete suggestion.
For example,
I am have directories with files such as these:
LSFJOB_647169535/ LSFJOB_647158534/
In this case, if I type LSF and hit Tab, then I get:
LSFJOB_6471
But then I have to focus hard to get which character should I type next.
I'd like bash to suggest me something like:
LSFJOB_647169535/ LSFJOB_647158534/
or
LSFJOB_647169535/ LSFJOB_647158534/
Do you know a way of doing it?
That is a good question!
Dennis Williamson already answered it there (SuperUser).
So it turns out that there is a "ReadLine Variable" that does exactly that: colored-completion-prefix.
Sadly it's only available in Bash v4.4 :c Link to the diff
You can check its value with bind -v|grep color
I tried to play with compgen but it appears that it strips colors away /:
Instead of colored-completion-prefix, which requires Bash 4.4, you could add the older (Bash 4.0)
set completion-prefix-display-length 2
to your ~/.inputrc (see manual). This replaces any common prefix longer than 2 characters with an ellipsis when showing the completions:
$ ls
LSFJOB_647158534 LSFJOB_647169535
$ cd LSFJOB_6471<tab>
...58534/ ...69535/

man command output full of ^H in vim

this question seems hard to search answers by google.
I executed:
man cp > cp.txt
vim cp.txt
there are many "^H" in the file, it maybe backspace, how to get rid of them?
Just like our grand parents with their typewriters, man uses "backspace" (^H) to to go over the previous character and type it again to obtain "bold" letters.
While you can get rid of those ^H with a simple :%s/<C-v><C-h>//g you will get many doubled characters:
"original
N^HNA^HAM^HME^HE
"result
NNAAMMEE
So you would need to include the character just before or just after the ^H:
:%s/<C-v><C-h>.//g
But why go through all that trouble when you can obtain a clean text file directly with:
$ man cp | col -b > cp.txt
Those special control characters are probably ANSI escape sequences, used to add styling and color to the text. Vim doesn't parse those by default, but the AnsiEsc plugin uses Vim's conceal and syntax highlighting features to draw the text as intended.
Vim ships with a Man page reader. Add the following to your ~/.vimrc:
runtime ftplugin/man.vim
Now you can do :Man cp to open a man page on cp.
See :h :Man for more help.

Sublimetext2 work with python console

I try to write two or more lines of code in python console in SublimeText2 editor and I get the following error when I press enter after ":"
>>> for i in dir():
File "<string>", line 1
for i in dir():
^
SyntaxError: unexpected EOF while parsing
Is this a bug or did I do something wrong? And another question is any way to use multiline methods in console?
The Sublime Text console is not meant to be used for coding. It is there mostly for troubleshooting and plugin output. Though you input multiple lines by using ;, I highly recommend against this. Will Bond does this to install Package Control, which if you have't downloaded already, I recommend you do. If you're looking for a python REPL, check out Wuub's excellent plugin SublimeREPL, which will give you a terminal/console like experience for quick python input.

Resources