Text fragments are a handy way to target a specific part of a website through an URL based on its text. Its syntax is just really hard to remember:

#:~:text=[prefix-,]textStart[,textEnd][,-suffix]

So I wrote a script using jq’s @uri to percent-encode the text part!

#!/bin/bash
 
# Creates a text fragment to append to URIs
# See: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Fragment/Text_fragments
 
set -euo pipefail
 
if ! command -v jq >/dev/null 2>&1
then
    echo "jq is required. See https://jqlang.org/."
    exit 1
fi
 
encoded_arg=$(echo "$1" | jq -sRr @uri)
echo "#:~:text=$encoded_arg"

Now, I can run ,fragment text (see Pesky little scripts for naming convention) and get the result that I can then pipe to | pbcopy on Mac to copy to command line and append to a URL.