Add Line Numbers to a Hugo Code Block
Hugo can add line numbers to one code block without changing every snippet on the site. Add linenos=true to the opening fence.
Here is the same small example without line numbers:
print("Hello, World!")
Now enable them for that block:
print("Hello, World!")
If the numbers do not appear
Hugo’s syntax highlighter produces the line-number markup, while the theme is responsible for making it look right. If nothing changes, check that:
- the Hugo version you use supports the option;
- syntax highlighting is enabled in the site configuration;
- the theme includes styles for highlighted code and line numbers;
- the opening fence is written exactly as shown above.
I would not enable line numbers for every one-line example. They are useful when the article refers readers to particular lines; otherwise, plain code blocks are usually easier to scan.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Customizing Hugo’s RSS Feed
Adjust Hugo RSS titles, descriptions, content, and section filtering while keeping the generated feed valid and easy to discover.
- Organizing Hugo Content With Tags and Categories
Organize Hugo content with a small, deliberate set of tags, categories, or custom taxonomies that lead to useful archive pages.
- A More Useful 404 Page in Hugo
Turn Hugo's 404 page into a useful route back to the homepage, search, or content visitors may have wanted.