Jacob Tomlinson's profile picture Jacob Tomlinson
Home Blog Talks Newsletter About

How to highlight lines in a Hugo code block

1 minute read #hugo, #syntax, #static-site-generators, #blogging, #quick-tips

Sometimes when writing code in a blog post I want to emphasize a couple of lines in particular. Today I found out that Hugo has really nice syntax to do this in a regular markdown code-fence.

I prefer to use code-fences over the highlight shortcode for code blocks because I get syntax highlighting of the code within the code-fence in my editor.

Here I am emphasizing line 5 that contains the the print statement.

import datetime

def main():
    current_time = datetime.datetime.now().strftime("%H:%M")
    print(f"Hello at {current_time}!")

if __name__ == "__main__":
    main()

To do this I included a little extra information as part of my triple-backtick code-fence.

```python {hl_lines=[5]}
import datetime

def main():
    current_time = datetime.datetime.now().strftime("%H:%M")
    print(f"Hello at {current_time}!")

if __name__ == "__main__":
    main()
```

To learn more check out the Hugo syntax highlighting documentation.


Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!