Highlight and Search – Google Search from Any Ubuntu Linux Application

Some time ago, I wrote a post to show how to modify terminator to enable easy google searching. However I have trumped myself and have an even better solution now. Google Search from any application. Modus Operandi is: Highlight Text => Press Key Combination => Script picks up the query from the clipboard => Formats the query and opens the URL in your defualt browser.

Lets start with the script. Copy-Paste the below code into a file (Lets name it google_search).

#!/bin/bash
QUERY=`xclip -o | tr ' ' '+'`  #sanitize the query, change spaces to the plus sign
QUERY='"'$QUERY'"'    #put quotes around the query, exact search

if [ -x ~/bin/open_browser.sh ]; then
  open_browser.sh http://www.google.com/webhp?hl=en#hl=en\&q=$QUERY
else
  x-www-browser http://www.google.com/webhp?hl=en#hl=en\&q=$QUERY
fi

Copy the file to the bin directory under your home directory. Make the script executable.

cp google_search ~/bin
chmod +x ~/bin/google_search

Now lets take care of a few dependencies.

Install xclip. This utility is used to interact with the clipboard.

sudo aptitude install xclip

Update x-www-browser. See this tutorial for more instructions.

sudo update-alternatives --config x-www-browser

Now lets create a keyboard shortcut for the script we saved earlier.

Tutorial for KDE Users
Tutorial for Gnome Users

Thats it. Now you can highlight any text and press the keyboard shortcut you created in the last step and a google search for the selected phrase will open up in your default browser.

This script also supports the open_browser.sh script. Look here for more info.
When you press the keyboard combination, the browser may or may not become the active window. That depends on the focus model. However if you check your browser, the google search should be open in a new tab

Terminator Patched to Support Google Search Through Right Click Menu

Besides xterm which is my all time favourite terminal app, Terminator is the other terminal app that I use on an everyday basis. I do a lot of coding and compiling in Terminator using vim. Its been an annoyance ever since I started using Linux (which is about 10 years now), that everytime I have to google an error, there’s no way of doing it in one singe step from any existing terminal app. Today I had an especially rough day with compilation errors and the repeated copy/pasting into google irritated the hell out me. Thats when I decided to add this support to Terminator knowing that its a python app and should be relatively simple to modify. It was infact quite easy to add the support and you can see the result of the effort below. Continue reading