Syntax highlighting
Add syntax highlighting to code examples.
Hugo provides several methods to add syntax highlighting to code examples:
- Use the
transform.Highlightfunction within your templates - Use the
highlightshortcode with any content format - Use fenced code blocks with the Markdown content format
Fenced code blocks
In its default configuration, Hugo highlights code examples within fenced code blocks, following this form:
content/example.md
```LANG [OPTIONS]
CODE
```- CODE
- The code to highlight.
- LANG
- The language of the code to highlight. Choose from one of the supported languages. This value is case-insensitive.
- OPTIONS
- One or more space-separated or comma-separated key-value pairs wrapped in braces. Set default values for each option in your site configuration. The key names are case-insensitive.
For example, with this Markdown:
content/example.md
```go {linenos=inline hl_lines=[3,"6-8"] style=emacs}
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Value of i:", i)
}
}
```Hugo renders this:
1package main
2
3import "fmt"
4
5func main() {
6 for i := 0; i < 3; i++ {
7 fmt.Println("Value of i:", i)
8 }
9}Options
- anchorLineNos
- (
bool) Whether to render each line number as an HTML anchor element, setting theidattribute of the surroundingspanelement to the line number. Irrelevant iflineNosisfalse. Default isfalse. - codeFences
- (
bool) Whether to highlight fenced code blocks. Default istrue. - guessSyntax
- (
bool) Whether to automatically detect the language if theLANGargument is blank or set to a language for which there is no corresponding lexer. Falls back to a plain text lexer if unable to automatically detect the language. Default isfalse.The Chroma syntax highlighter includes lexers for approximately 250 languages, but only 5 of these have implemented automatic language detection.
- hl_Lines
- (
string) A space-delimited list of lines to emphasize within the highlighted code. To emphasize lines 2, 3, 4, and 7, set this value to2-4 7. This option is independent of thelineNoStartoption. - hl_inline
- (
bool) Whether to render the highlighted code without a wrapping container. Default isfalse. - lineAnchors
- (
string) When rendering a line number as an HTML anchor element, prepend this value to theidattribute of the surroundingspanelement. This provides uniqueidattributes when a page contains two or more code blocks. Irrelevant iflineNosoranchorLineNosisfalse. - lineNoStart
- (
int) The number to display at the beginning of the first line. Irrelevant iflineNosisfalse. Default is1. - lineNos
- (
any) Controls line number display. Default isfalse.true: Enable line numbers, controlled bylineNumbersInTable.false: Disable line numbers.inline: Enable inline line numbers (setslineNumbersInTabletofalse).table: Enable table-based line numbers (setslineNumbersInTabletotrue).
- lineNumbersInTable
- (
bool) Whether to render the highlighted code in an HTML table with two cells. The left table cell contains the line numbers, while the right table cell contains the code. Irrelevant iflineNosisfalse. Default istrue. - noClasses
- (
bool) Whether to use inline CSS styles instead of an external CSS file. Default istrue. To use an external CSS file, set this value tofalseand generate the CSS file from the command line:hugo gen chromastyles --style=monokai > syntax.css - style
- (
string) The CSS styles to apply to the highlighted code. Case-sensitive. Default ismonokai. See syntax highlighting styles. - tabWidth
- (
int) Substitute this number of spaces for each tab character in your highlighted code. Irrelevant ifnoClassesisfalse. Default is4. - wrapperClass
- New in v0.140.2
- (
string) The class or classes to use for the outermost element of the highlighted code. Default ishighlight.
Escaping
When documenting shortcode usage, escape the tag delimiters:
content/example.md
```text {linenos=inline}
{{</* shortcode-1 */>}}
{{%/* shortcode-2 */%}}
```Hugo renders this to:
1{{< shortcode-1 >}}
2
3{{% shortcode-2 %}}Languages
These are the supported languages. Use one of the identifiers, not the language name, when specifying a language for:
- The
transform.Highlightfunction - The
highlightshortcode - Fenced code blocks
| Language | Identifiers |
|---|---|
| ABAP | abap |
| ABNF | abnf |
| ActionScript | as,
actionscript |
| ActionScript 3 | as3,
actionscript3 |
| Ada | ada,
ada95,
ada2005 |
| Agda | agda |
| AL | al |
| Alloy | alloy |
| Angular2 | ng2 |
| ANTLR | antlr |
| ApacheConf | apacheconf,
aconf,
apache |
| APL | apl |
| AppleScript | applescript |
| ArangoDB AQL | aql |
| Arduino | arduino |
| ArmAsm | armasm |
| ATL | atl |
| AutoHotkey | autohotkey,
ahk |
| AutoIt | autoit |
| Awk | awk,
gawk,
mawk,
nawk |
| Ballerina | ballerina |
| Bash | bash,
sh,
ksh,
zsh,
shell |
| Bash Session | bash-session,
console,
shell-session |
| Batchfile | bat,
batch,
dosbatch,
winbatch |
| Beef | beef |
| BibTeX | bib,
bibtex |
| Bicep | bicep |
| BlitzBasic | blitzbasic,
b3d,
bplus |
| BNF | bnf |
| BQN | bqn |
| Brainfuck | brainfuck,
bf |
| C | c |
| C# | csharp,
c# |
| C++ | cpp,
c++ |
| Caddyfile | caddyfile,
caddy |
| Caddyfile Directives | caddyfile-directives,
caddyfile-d,
caddy-d |
| Cap'n Proto | capnp |
| Cassandra CQL | cassandra,
cql |
| Ceylon | ceylon |
| CFEngine3 | cfengine3,
cf3 |
| cfstatement | cfs |
| ChaiScript | chai,
chaiscript |
| Chapel | chapel,
chpl |
| Cheetah | cheetah,
spitfire |
| Clojure | clojure,
clj,
edn |
| CMake | cmake |
| COBOL | cobol |
| CoffeeScript | coffee-script,
coffeescript,
coffee |
| Common Lisp | common-lisp,
cl,
lisp |
| Coq | coq |
| Core | core |
| Crystal | cr,
crystal |
| CSS | css |
| CSV | csv |
| CUE | cue |
| Cython | cython,
pyx,
pyrex |
| D | d |
| Dart | dart |
| Dax | dax |
| Desktop file | desktop,
desktop_entry |
| Diff | diff,
udiff |
| Django/Jinja | django,
jinja |
| dns | zone,
bind |
| Docker | docker,
dockerfile,
containerfile |
| DTD | dtd |
| Dylan | dylan |
| EBNF | ebnf |
| Elixir | elixir,
ex,
exs |
| Elm | elm |
| EmacsLisp | emacs,
elisp,
emacs-lisp |
| Erlang | erlang |
| Factor | factor |
| Fennel | fennel,
fnl |
| Fish | fish,
fishshell |
| Forth | forth |
| Fortran | fortran,
f90 |
| FortranFixed | fortranfixed |
| FSharp | fsharp |
| GAS | gas,
asm |
| GDScript | gdscript,
gd |
| GDScript3 | gdscript3,
gd3 |
| Gemtext | gemtext,
gmi,
gmni,
gemini |
| Genshi | genshi,
kid,
xml+genshi,
xml+kid |
| Genshi HTML | html+genshi,
html+kid |
| Genshi Text | genshitext |
| Gherkin | cucumber,
Cucumber,
gherkin,
Gherkin |
| Gleam | gleam |
| GLSL | glsl |
| Gnuplot | gnuplot |
| Go | go,
golang |
| Go HTML Template | go-html-template |
| Go Template | go-template |
| Go Text Template | go-text-template |
| GraphQL | graphql,
graphqls,
gql |
| Groff | groff,
nroff,
man |
| Groovy | groovy |
| Handlebars | handlebars,
hbs |
| Hare | hare |
| Haskell | haskell,
hs |
| Haxe | hx,
haxe,
hxsl |
| HCL | hcl |
| Hexdump | hexdump |
| HLB | hlb |
| HLSL | hlsl |
| HolyC | holyc |
| HTML | html |
| HTTP | http |
| Hy | hylang |
| Idris | idris,
idr |
| Igor | igor,
igorpro |
| INI | ini,
cfg,
dosini |
| Io | io |
| ISCdhcpd | iscdhcpd |
| J | j |
| Janet | janet |
| Java | java |
| JavaScript | js,
javascript |
| JSON | json |
| JSONata | jsonata |
| Jsonnet | jsonnet |
| Julia | julia,
jl |
| Jungle | jungle |
| Kotlin | kotlin |
| Lean4 | lean4,
lean |
| Lighttpd configuration file | lighty,
lighttpd |
| LLVM | llvm |
| lox | |
| Lua | lua,
luau |
| Makefile | make,
makefile,
mf,
bsdmake |
| Mako | mako |
| markdown | md,
mkd |
| Mason | mason |
| Materialize SQL dialect | materialize,
mzsql |
| Mathematica | mathematica,
mma,
nb |
| Matlab | matlab |
| MCFunction | mcfunction,
mcf |
| Meson | meson,
meson.build |
| Metal | metal |
| MiniZinc | minizinc,
MZN,
mzn |
| MLIR | mlir |
| Modula-2 | modula2,
m2 |
| Mojo | mojo,
🔥 |
| MonkeyC | monkeyc |
| MoonScript | moonscript,
moon |
| MorrowindScript | morrowind,
mwscript |
| Myghty | myghty |
| MySQL | mysql,
mariadb |
| NASM | nasm |
| Natural | natural |
| NDISASM | ndisasm |
| Newspeak | newspeak |
| Nginx configuration file | nginx |
| Nim | nim,
nimrod |
| Nix | nixos,
nix |
| NSIS | nsis,
nsi,
nsh |
| Nu | nu |
| Objective-C | objective-c,
objectivec,
obj-c,
objc |
| ObjectPascal | objectpascal |
| OCaml | ocaml |
| Octave | octave |
| Odin | odin |
| OnesEnterprise | ones,
onesenterprise,
1S,
1S:Enterprise |
| OpenEdge ABL | openedge,
abl,
progress,
openedgeabl |
| OpenSCAD | openscad |
| Org Mode | org,
orgmode |
| PacmanConf | pacmanconf |
| Perl | perl,
pl |
| PHP | php,
php3,
php4,
php5 |
| PHTML | phtml |
| Pig | pig |
| PkgConfig | pkgconfig |
| PL/pgSQL | plpgsql |
| plaintext | text,
plain,
no-highlight |
| Plutus Core | plutus-core,
plc |
| Pony | pony |
| PostgreSQL SQL dialect | postgresql,
postgres |
| PostScript | postscript,
postscr |
| POVRay | pov |
| PowerQuery | powerquery,
pq |
| PowerShell | powershell,
posh,
ps1,
psm1,
psd1,
pwsh |
| Prolog | prolog |
| Promela | promela |
| PromQL | promql |
| properties | java-properties |
| Protocol Buffer | protobuf,
proto |
| PRQL | prql |
| PSL | psl |
| Puppet | puppet |
| Python | python,
py,
sage,
python3,
py3 |
| Python 2 | python2,
py2 |
| QBasic | qbasic,
basic |
| QML | qml,
qbs |
| R | splus,
s,
r |
| Racket | racket,
rkt |
| Ragel | ragel |
| Raku | perl6,
pl6,
raku |
| react | jsx,
react |
| ReasonML | reason,
reasonml |
| reg | registry |
| Rego | rego |
| reStructuredText | rst,
rest,
restructuredtext |
| Rexx | rexx,
arexx |
| RPGLE | SQLRPGLE,
RPG IV |
| RPMSpec | spec |
| Ruby | rb,
ruby,
duby |
| Rust | rust,
rs |
| SAS | sas |
| Sass | sass |
| Scala | scala |
| Scheme | scheme,
scm |
| Scilab | scilab |
| SCSS | scss |
| Sed | sed,
gsed,
ssed |
| Sieve | sieve |
| Smali | smali |
| Smalltalk | smalltalk,
squeak,
st |
| Smarty | smarty |
| SNBT | snbt |
| Snobol | snobol |
| Solidity | sol,
solidity |
| SourcePawn | sp |
| SPARQL | sparql |
| SQL | sql |
| SquidConf | squidconf,
squid.conf,
squid |
| Standard ML | sml |
| stas | |
| Stylus | stylus |
| Svelte | svelte |
| Swift | swift |
| SYSTEMD | systemd |
| systemverilog | systemverilog,
sv |
| TableGen | tablegen |
| Tal | tal,
uxntal |
| TASM | tasm |
| Tcl | tcl |
| Tcsh | tcsh,
csh |
| Termcap | termcap |
| Terminfo | terminfo |
| Terraform | terraform,
tf,
hcl |
| TeX | tex,
latex |
| Thrift | thrift |
| TOML | toml |
| TradingView | tradingview,
tv |
| Transact-SQL | tsql,
t-sql |
| Turing | turing |
| Turtle | turtle |
| Twig | twig |
| TypeScript | ts,
tsx,
typescript |
| TypoScript | typoscript |
| TypoScriptCssData | typoscriptcssdata |
| TypoScriptHtmlData | typoscripthtmldata |
| Typst | typst |
| ucode | |
| V | v,
vlang |
| V shell | vsh,
vshell |
| Vala | vala,
vapi |
| VB.net | vb.net,
vbnet |
| verilog | verilog,
v |
| VHDL | vhdl |
| VHS | vhs,
tape,
cassette |
| VimL | vim |
| vue | vue,
vuejs |
| WDTE | |
| WebGPU Shading Language | wgsl |
| WebVTT | vtt |
| Whiley | whiley |
| XML | xml |
| Xorg | xorg.conf |
| YAML | yaml |
| YANG | yang |
| Z80 Assembly | z80 |
| Zed | zed |
| Zig | zig |
Last updated:
March 6, 2025
:
theme: Restore deduplication logic for related items (5f32c92ed)
Improve this page