Tuesday, January 31, 2017

Google from the command line

It occurred to me that for Google searches where I don't need suggestions I can do a search by hitting a URL from the command line with this one-line shell script:

% cat gg
open https://www.google.com/search?q="$*"

Let's make one for Wikipedia, too:

% cat wk
open http://en.wikipedia.org/wiki/"$*"

Yup, I should generalize those to a script that looks at its name and maps that to an appropriate URL. Here it is:

% cat hitit
which=$(basename $0)
case $which in
    gg)
        url="https://www.google.com/search?q=$*"
        ;;
        
    wk)
        url="http://en.wikipedia.org/wiki/$*"
        ;;
esac

open "$url"

Let's add a couple of symlinks in my sbin directory:

$ ln -s hitit gg
$ ln -s hitit wk

Now, instead of

  1. Switch to Chrome (I use cmd-, 3 with Keyboard Maestro)
  2. Type cmd-L to get to the address field
  3. Type the search string, or use a Chrome "search engine" like w to hit Wikipedia.
I can do this:
% gg astroboy
% wk ruby lang

The above use open(1) on OS X but on Cygwin, which I may be moving back to, I'd hit URLs from the command line with this script:

% cat ffx
"/c/Program Files/Mozilla Firefox/firefox.exe" $*

No comments: