2021-09-28 18:44:59 +00:00
---
title: Code Blocks
---
2022-05-22 20:00:37 +00:00
There are several ways to add code blocks. Most of them work out of the box, only the Hugo shortcode `<highlight>` needs to be configured to work properly. The theme also provides some additional features like a copy button and an option to set the maximum length of code blocks. Both of these functions and the dependent formatting rely on the `.highlight` CSS class. You must ensure that you always assign a language to your code blocks if you want to use these functions. If you do not want to apply syntax highlighting, you can also specify `plain` or `text` as the language.
2020-01-12 14:33:02 +00:00
2020-12-22 12:15:36 +00:00
{{< toc > }}
2020-01-12 14:33:02 +00:00
## Inline code
2020-06-01 22:01:49 +00:00
2020-01-12 14:33:02 +00:00
To display an inline shortcode use single quotes:
2020-06-01 22:01:49 +00:00
```plain
2020-01-12 14:33:02 +00:00
`some code`
```
2022-02-03 10:42:26 +00:00
**Example:** `some code` with a [`link` ](# )
2020-01-12 14:33:02 +00:00
## Code blocks
2020-06-01 22:01:49 +00:00
2020-05-31 16:17:32 +00:00
Code blocks can be uses without language specification:
2020-01-12 14:33:02 +00:00
2020-06-01 22:01:49 +00:00
````markdown
2021-11-07 21:12:14 +00:00
```Plain
2020-01-12 14:33:02 +00:00
some code
```
````
**Example:**
2021-11-07 21:12:14 +00:00
```Plain
2020-01-12 14:33:02 +00:00
some code
```
... or if you need language specific syntax highlighting:
2020-06-01 22:01:49 +00:00
````markdown
2020-01-12 14:33:02 +00:00
```Shell
# some code
echo "Hello world"
```
````
**Example:**
```Shell
# some code
echo "Hello World"
```
## Highlight shortcode
2020-05-31 16:17:32 +00:00
Hugo has a build-in shortcode for syntax highlighting. To work properly with this theme, you have to set following options in your site configuration:
2020-01-12 14:33:02 +00:00
{{< tabs " uniqueid " > }}
{{< tab " TOML " > }}
2020-06-01 22:01:49 +00:00
2020-01-12 14:33:02 +00:00
```TOML
pygmentsUseClasses=true
pygmentsCodeFences=true
```
2020-06-01 22:01:49 +00:00
2020-01-12 14:33:02 +00:00
{{< / tab > }}
{{< tab " YAML " > }}
2020-06-01 22:01:49 +00:00
2020-01-12 14:33:02 +00:00
```YAML
pygmentsUseClasses: true
pygmentsCodeFences: true
```
2020-06-01 22:01:49 +00:00
2020-01-12 14:33:02 +00:00
{{< / tab > }}
{{< / tabs > }}
You can use it like every other shortcode:
2020-09-10 20:23:24 +00:00
<!-- prettier - ignore -->
2020-06-01 22:01:49 +00:00
```markdown
2020-01-12 14:33:02 +00:00
{{< /* highlight Shell "linenos=table" */>}}
# some code
echo "Hello World"
{{< /* /highlight */>}}
```
**Example:**
2020-09-10 20:23:24 +00:00
<!-- prettier - ignore - start -->
2022-05-23 22:37:06 +00:00
<!-- markdownlint - capture -->
<!-- markdownlint - disable -->
2020-01-12 14:33:02 +00:00
{{< highlight Shell " linenos = table" > }}
# some code
echo "Hello World"
{{< / highlight > }}
2022-05-23 22:37:06 +00:00
<!-- markdownlint - restore -->
2020-09-10 20:23:24 +00:00
<!-- prettier - ignore - end -->
2022-05-23 22:37:06 +00:00
## Gist Shortcode
The Gist shortcode is a built-in Hugo shortcode to load GitHub gists. For details usage information please check the Hugo [documentation ](https://gohugo.io/content-management/shortcodes/#gist ).
<!-- prettier - ignore -->
```markdown
{{< /* gist spf13 7896402 */>}}
```
**Example:**
{{< gist spf13 7896402 > }}