How to prettify HTML documents at the terminal

Print Friendly, PDF & Email

When working with RESTful APIs, developers often hit API endpoints to test and look up data in either JSON or XML as the common formats. We have published two recent posts about prettifying JSON or XML documents on console windows if these documents are returned by the curl command to be used to call API endpoints. However, we sometimes need to open an HTML document from the terminal to perform a quick search or check. Many developers want to display this HTML document or its partial content in a nicer visualisation. We show in this post a tool published recently to support the format of HTML codes on the terminal.

What is the htmlq command?

Like jq, but for HTML. Uses CSS selectors to extract bits of content from HTML files. In the post context, we present the option of this command which supports printing a piece of HTML code in a better view.

Installation and usage


You can find all instructions for installation and usage on the tool’s website:

On MacOS, just run this command: brew install htmlq to install htmlq on your machine.


Pretty-print an HTML file

$ cat test.html | htmlq -p

    <title>My web page
      Hello, world!
      This is my first web page.
      It contains a <strong>main heading</strong> and <em> paragraph </em>.

Pretty-print a webpage

$ curl --silent | htmlq --pretty '#posts'

<section id="posts"><a class="header-link" href="/weblog/">
  <ul class="post-list">
      <div class="post-title">
        <div class="post-title-link"><a class="header-link" href="">
            <h3>Adventures in digital radio
        <div class="deemphasise post-timestamp">
          <time datetime="2022-12-22" pubdate="">
        <p class="post-description">
          New ways to talk to people about the weather
<p class="more">
    <a href="/weblog/">
      More posts</a>


That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.