Saturday, April 5, 2008

Search shortcuts for text based browsers (with bash)

Recently, I started experimenting with text-based web browsers (such as elinks or w3m). Of course, I still use firefox for pages that are more complex or need images, but sometimes it is nice to just have the text version in a console (no distractions, consistent colors, and ad blocking!)

However, one thing I really missed from firefox was the ease with which I could search google, wikipedia. or other sites. Well, with a bash script and some aliases, I can now do basically the same thing with a text based browser.

Here is the bash script:

#!/bin/bash
# Open a search for on in a text-based browser
# Usage: search.sh

browser=elinks # could also put w3m here

site=$1
query=$2%20$3%20$4%20$5%20$6%20$7%20$8%20$9 # hack to pick up >1 search terms

case $site in
"google" ) $browser 'http://www.google.com/search?q='$query;;
"wikipedia" ) $browser 'http://en.wikipedia.org/wiki/Special\:Search?search='$query;;
"torrentz" ) $browser 'http://www.torrentz.com/search?q='$query;;
"isohunt" ) $browser 'http://isohunt.com/torrents/?ihq='$query;;
"portage" ) $browser 'http://gentoo-portage.com/Search?search='$query;;
* ) echo "Unknown site";;
esac


I then added the following aliases to my .bashrc (the script is in ~/bin/)

alias google='~/bin/search.sh google'
alias wiki='~/bin/search.sh wikipedia'
alias torrentz='~/bin/search.sh torrentz'
alias iso='~/bin/search.sh isohunt'
alias port='~/bin/search.sh portage'


Now, if I want to quickly look up, say, a package in portage, I just type portage package_name in my terminal.

No comments: