Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Extract code blocks by language

Goal: Pull out only the code blocks written in a specific language, e.g. to review all the JavaScript snippets in a doc.

Prerequisites: None.

Query

select(.code.lang == "js")
$ mq 'select(.code.lang == "js")' README.md

Input

```js
const x = 1;
```

```python
x = 1
```

```js
const y = 2;
```

Output

Returns only the two JavaScript code blocks (const x = 1; and const y = 2;), dropping the Python one.

Notes

  • Shorthand: .code("js") selects the same nodes as select(.code.lang == "js") — the language selector doubles as a filter when called with an argument.
  • To see which languages appear in a document at all, use .code.lang on its own — it returns a list like ["js", "python", "rust", "bash"].
  • To strip code blocks out and keep only prose, invert the condition: select(!.code).