URL queries

So called «smart bookmarks» (i.e. the abbreviated aliases used to make queries easier) are certainly useful. Indeed, I've used them for years and have come to depend on them, but all of the implementations I've examined were missing one useful thing or another, and the data can be difficult to import/export between browsers.

Even if it did work as I wish in my preferred browser, separation remains essential, and this tool provides that independence along with the following features:

download
url-query script and example plain text configuration file
basic usage

The url-query script accepts a key and query-text substitution part(s).

url-query key q...

e.g. Search Wikipedia for the phrase 'robert anton wilson':

url-query w 'robert anton wilson'

multi-part e.g. Language translation (from, to, text):

url-query tr en fr 'keep it simple'
text selection

The current text selection can be used directly (i.e. no need to copy/paste). Option -s, with one or more keys as arguments, uses the text selection as the query-text (i.e. singular substitution part) for each URL.

url-query -s key...
tolerant interpretation
If there are more query-text parts than placeholders (i.e., extra arguments), the extra parts are taken as a single unit to substitute the last placeholder, after substituting (one to one) for previous placeholders (i.e. quotes are not required, but they can be used as need be).
If query-text is not given, the URL is loaded anyway (but only the base domain is loaded if the URL contains a placeholder).
quoting

Option -q toggles quoting. If the text selection contains spaces, it can be automatically quoted by setting the default value in the script, or by using option -q (which simply toggles the default).

url-query -q key...
print

Option -p prints each URL to standard output instead of opening it.

url-query -p key q...
menu
Option -m prints a list of keys and names suitable for use with an external menu/selector (such as dmenu, &c.). It accepts an optional format string with the following substitutions: {k} → key, {n} → name
e.g. {k} - {n}ia - Internet Archive
bookmarks
Option -b prints an HTML bookmarks file with SHORCUTURL=key for use in a browser (especially one which supports "smart bookmarks", "URL shortcuts", or "bookmark keywords", e.g. Firefox).
new records

Option -n can be used to add a new record (which will test to avoid duplicates, and prompt for a new key if necessary).

url-query -n key name URL-or-key(s)
url-query -n ted 'TED talks' 'https://www.ted.com/search?q=%s'
key bindings
I use global key bindings for the most frequent queries (e.g. search, dictionary, encyclopedia, video, image, map, &c.). Two more prompt for input (using a GUI keyboard input tool like yad, &c.); one calls url-query, the other calls url-query -s. A third binding displays a menu (e.g. dmenu) of all query sources (useful when one has forgotten the abbreviation).
dependencies
The script is written in ksh because it's fast (more than 2 times faster than bash in this case) and useful, but it also runs under bash (as provided here for convenience). External commands are used to print the text selection (e.g. xsel), to open the url(s) (e.g. xdg-open), and to execute awk script. I use the excellent mawk (often even faster than sed!) which makes this script run several times faster than using gawk (the usual default awk).
related software
Surfraw is a similar tool but it doesn't do what I want.