URL queries

So called «bookmark keywords» (i.e. the abbreviated aliases used to make queries easier) are certainly useful. I’ve been using them for years and have come to depend on them, but all of the implementations I’ve used 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 is useful, and this tool provides that independence, in addition to the following features:

See the example plain text configuration file.

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

url-query key p...

Example, search Wikipedia for the phrase ‘robert anton wilson’:

url-query w 'robert anton wilson'

Multi-part example, 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). The option -s, with one or more keys as arguments, uses the text selection as the (singular) substitution part for each URL.

url-query -s key...
tolerant interpretation
If there are more 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 any previous placeholders (i.e. quotes are not required, but they can be used as need be).
If no substitution parts are given, the URL is loaded anyway (but only the base domain is loaded if the URL contains a placeholder).

The 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 the command option (which simply toggles the default).

url-query -q key...
new records

The option -n can be used to add a new record (which is useful because it tests to avoid duplicates, and prompts for a new key if necessary).

url-query -n key name URL-or-key(s)
url-query -n ted 'TED talks' 'http://www.ted.com/search?q=%s'
test mode

The option -t invokes test mode: the command for each URL is printed to standard output instead of being evaluated.

url-query -t key p...
The script uses ksh (because it’s fast and useful) but it also works with bash. The text selection is accessed using xsel. The rest are basic Unix tools. Cheers to the excellent mawk (often even faster than sed)!
key bindings
I use global key bindings for the most frequent queries (e.g. search, dictionary, encyclopedia, video, image, map, &c.), and also two which prompt for input (using a tool like zenity, or a menu, &c.); one calls url-query, the other calls url-query -s.
related software
Surfraw is a similar tool but it doesn’t do what I want.