So called «smart bookmarks» (i.e. the abbreviated aliases used to make queries easier) are certainly useful. Indeed, I've used them for decades 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 in addition to:
The script is written in ksh1, but it also runs under bash (as provided here for convenience). External commands are used to open the url(s) (e.g. xdg-open), and to execute awk script.2
The url-query script accepts a key and query-text substitution part(s).
url-query key q...
Search Wikipedia for the phrase 'robert anton wilson':
url-query w 'robert anton wilson'
Multi-part substitution – language translation (from, to, text):
url-query tr en fr 'keep it simple'
Option -s
uses the first argument as query-text and remaining argument(s) as key(s) enabling multiple keys to be queried for the same text.
url-query -s q key...
If there are more query-text arguments than placeholders, the extra arguments are taken as a single unit to substitute the last placeholder, after substituting (one to one) for previous placeholders.
Query-text receives newline → space substitution (unless disabled by option -S
).
If query-text is not given and the URL contains a placeholder, the URL is reduced to base domain.
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
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).
Option -p
prints each URL to standard output instead of opening it.
I use global key bindings for the most frequent queries (e.g. search, dictionary, encyclopedia, video, image, map, &c.). Two more bindings prompt for input (using a GUI keyboard input tool like yad, &c.); one calls url-query
, the other calls url-query -s "$(xsel)"
using the text selection as query-text with one or more keys. A third binding also uses the text selection but displays a menu of all query sources (useful when one has forgotten the abbreviation).
If the configuration file does not exist, a new file is created with a brief explanation and examples.
The configuration file uses a plain-text tsv format:
key name target
target: url | key...
Target url can contain one or more place holders (for given query text):
%s
→ url-encoded
%S
→ verbatim
Blank lines and comments (i.e. lines beginning with #) are ignored
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 wy 'Wiby search' 'https://wiby.me/?q=%s'
Option -v
verifies the configuration file, testing for duplicate keys.
Option -c
specifies the configuration file path (to override the default).
Surfraw is a similar tool but it doesn't do what I want.