HUGO
News Docs Themes Community GitHub

Syntax highlighting

Add syntax highlighting to code examples.

Hugo provides several methods to add syntax highlighting to code examples:

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 the id attribute of the surrounding span element to the line number. Irrelevant if lineNos is false. Default is false.
codeFences
(bool) Whether to highlight fenced code blocks. Default is true.
guessSyntax
(bool) Whether to automatically detect the language if the LANG argument 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 is false.

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 to 2-4 7. This option is independent of the lineNoStart option.
hl_inline
(bool) Whether to render the highlighted code without a wrapping container. Default is false.
lineAnchors
(string) When rendering a line number as an HTML anchor element, prepend this value to the id attribute of the surrounding span element. This provides unique id attributes when a page contains two or more code blocks. Irrelevant if lineNos or anchorLineNos is false.
lineNoStart
(int) The number to display at the beginning of the first line. Irrelevant if lineNos is false. Default is 1.
lineNos
(any) Controls line number display. Default is false.
  • true: Enable line numbers, controlled by lineNumbersInTable.
  • false: Disable line numbers.
  • inline: Enable inline line numbers (sets lineNumbersInTable to false).
  • table: Enable table-based line numbers (sets lineNumbersInTable to true).
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 if lineNos is false. Default is true.
noClasses
(bool) Whether to use inline CSS styles instead of an external CSS file. Default is true. To use an external CSS file, set this value to false and 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 is monokai. See syntax highlighting styles.
tabWidth
(int) Substitute this number of spaces for each tab character in your highlighted code. Irrelevant if noClasses is false. Default is 4.
wrapperClass
New in v0.140.2
(string) The class or classes to use for the outermost element of the highlighted code. Default is highlight.

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:

LanguageIdentifiers
ABAPabap
ABNFabnf
ActionScriptas, actionscript
ActionScript 3as3, actionscript3
Adaada, ada95, ada2005
Agdaagda
ALal
Alloyalloy
Angular2ng2
ANTLRantlr
ApacheConfapacheconf, aconf, apache
APLapl
AppleScriptapplescript
ArangoDB AQLaql
Arduinoarduino
ArmAsmarmasm
ATLatl
AutoHotkeyautohotkey, ahk
AutoItautoit
Awkawk, gawk, mawk, nawk
Ballerinaballerina
Bashbash, sh, ksh, zsh, shell
Bash Sessionbash-session, console, shell-session
Batchfilebat, batch, dosbatch, winbatch
Beefbeef
BibTeXbib, bibtex
Bicepbicep
BlitzBasicblitzbasic, b3d, bplus
BNFbnf
BQNbqn
Brainfuckbrainfuck, bf
Cc
C#csharp, c#
C++cpp, c++
Caddyfilecaddyfile, caddy
Caddyfile Directivescaddyfile-directives, caddyfile-d, caddy-d
Cap'n Protocapnp
Cassandra CQLcassandra, cql
Ceylonceylon
CFEngine3cfengine3, cf3
cfstatementcfs
ChaiScriptchai, chaiscript
Chapelchapel, chpl
Cheetahcheetah, spitfire
Clojureclojure, clj, edn
CMakecmake
COBOLcobol
CoffeeScriptcoffee-script, coffeescript, coffee
Common Lispcommon-lisp, cl, lisp
Coqcoq
Crystalcr, crystal
CSScss
CSVcsv
CUEcue
Cythoncython, pyx, pyrex
Dd
Dartdart
Daxdax
Desktop filedesktop, desktop_entry
Diffdiff, udiff
Django/Jinjadjango, jinja
dnszone, bind
Dockerdocker, dockerfile
DTDdtd
Dylandylan
EBNFebnf
Elixirelixir, ex, exs
Elmelm
EmacsLispemacs, elisp, emacs-lisp
Erlangerlang
Factorfactor
Fennelfennel, fnl
Fishfish, fishshell
Forthforth
Fortranfortran, f90
FortranFixedfortranfixed
FSharpfsharp
GASgas, asm
GDScriptgdscript, gd
GDScript3gdscript3, gd3
Genshigenshi, kid, xml+genshi, xml+kid
Genshi HTMLhtml+genshi, html+kid
Genshi Textgenshitext
Gherkincucumber, Cucumber, gherkin, Gherkin
Gleamgleam
GLSLglsl
Gnuplotgnuplot
Gogo, golang
Go HTML Templatego-html-template
Go Templatego-template
Go Text Templatego-text-template
GraphQLgraphql, graphqls, gql
Groffgroff, nroff, man
Groovygroovy
Handlebarshandlebars, hbs
Harehare
Haskellhaskell, hs
Haxehx, haxe, hxsl
HCLhcl
Hexdumphexdump
HLBhlb
HLSLhlsl
HolyCholyc
HTMLhtml
HTTPhttp
Hyhylang
Idrisidris, idr
Igorigor, igorpro
INIini, cfg, dosini
Ioio
ISCdhcpdiscdhcpd
Jj
Javajava
JavaScriptjs, javascript
JSONjson
JSONatajsonata
Jsonnetjsonnet
Juliajulia, jl
Junglejungle
Kotlinkotlin
Lighttpd configuration filelighty, lighttpd
LLVMllvm
Lualua
Makefilemake, makefile, mf, bsdmake
Makomako
markdownmd, mkd
Masonmason
Materialize SQL dialectmaterialize, mzsql
Mathematicamathematica, mma, nb
Matlabmatlab
MCFunctionmcfunction, mcf
Mesonmeson, meson.build
Metalmetal
MiniZincminizinc, MZN, mzn
MLIRmlir
Modula-2modula2, m2
MonkeyCmonkeyc
MorrowindScriptmorrowind, mwscript
Myghtymyghty
MySQLmysql, mariadb
NASMnasm
Naturalnatural
NDISASMndisasm
Newspeaknewspeak
Nginx configuration filenginx
Nimnim, nimrod
Nixnixos, nix
NSISnsis, nsi, nsh
Objective-Cobjective-c, objectivec, obj-c, objc
ObjectPascalobjectpascal
OCamlocaml
Octaveoctave
Odinodin
OnesEnterpriseones, onesenterprise, 1S, 1S:Enterprise
OpenEdge ABLopenedge, abl, progress, openedgeabl
OpenSCADopenscad
Org Modeorg, orgmode
PacmanConfpacmanconf
Perlperl, pl
PHPphp, php3, php4, php5
PHTMLphtml
Pigpig
PkgConfigpkgconfig
PL/pgSQLplpgsql
plaintexttext, plain, no-highlight
Plutus Coreplutus-core, plc
Ponypony
PostgreSQL SQL dialectpostgresql, postgres
PostScriptpostscript, postscr
POVRaypov
PowerQuerypowerquery, pq
PowerShellpowershell, posh, ps1, psm1, psd1, pwsh
Prologprolog
Promelapromela
PromQLpromql
propertiesjava-properties
Protocol Bufferprotobuf, proto
PRQLprql
PSLpsl
Puppetpuppet
Pythonpython, py, sage, python3, py3
Python 2python2, py2
QBasicqbasic, basic
QMLqml, qbs
Rsplus, s, r
Racketracket, rkt
Ragelragel
Rakuperl6, pl6, raku
reactjsx, react
ReasonMLreason, reasonml
regregistry
Regorego
reStructuredTextrst, rest, restructuredtext
Rexxrexx, arexx
RPMSpecspec
Rubyrb, ruby, duby
Rustrust, rs
SASsas
Sasssass
Scalascala
Schemescheme, scm
Scilabscilab
SCSSscss
Sedsed, gsed, ssed
Sievesieve
Smalismali
Smalltalksmalltalk, squeak, st
Smartysmarty
SNBTsnbt
Snobolsnobol
Soliditysol, solidity
SourcePawnsp
SPARQLsparql
SQLsql
SquidConfsquidconf, squid.conf, squid
Standard MLsml
stas
Stylusstylus
Sveltesvelte
Swiftswift
SYSTEMDsystemd
systemverilogsystemverilog, sv
TableGentablegen
Taltal, uxntal
TASMtasm
Tcltcl
Tcshtcsh, csh
Termcaptermcap
Terminfoterminfo
Terraformterraform, tf
TeXtex, latex
Thriftthrift
TOMLtoml
TradingViewtradingview, tv
Transact-SQLtsql, t-sql
Turingturing
Turtleturtle
Twigtwig
TypeScriptts, tsx, typescript
TypoScripttyposcript
TypoScriptCssDatatyposcriptcssdata
TypoScriptHtmlDatatyposcripthtmldata
Typsttypst
ucode
Vv, vlang
V shellvsh, vshell
Valavala, vapi
VB.netvb.net, vbnet
verilogverilog, v
VHDLvhdl
VHSvhs, tape, cassette
VimLvim
vuevue, vuejs
WDTE
WebGPU Shading Languagewgsl
WebVTTvtt
Whileywhiley
XMLxml
Xorgxorg.conf
YAMLyaml
YANGyang
Z80 Assemblyz80
Zedzed
Zigzig